Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用np.where使用自建函数向pandas添加列_Python_Pandas_Numpy - Fatal编程技术网

Python 使用np.where使用自建函数向pandas添加列

Python 使用np.where使用自建函数向pandas添加列,python,pandas,numpy,Python,Pandas,Numpy,我有一个熊猫数据框,有两列('no1'和'no2'),有些值包含汉字,有些不包含汉字 no1 no2 Paul Pogba 贝克汉姆 Gianluigi Buffon 莱奥内尔・梅西 莱奥内尔・梅西 莱奥内尔・梅西 Cristiano Ronaldo 莱奥内尔・梅西 STEVE HARRIS zinedine zidane Cristiano Ronaldo

我有一个熊猫数据框,有两列('no1'和'no2'),有些值包含汉字,有些不包含汉字

no1                     no2
Paul Pogba              贝克汉姆
Gianluigi Buffon        莱奥内尔・梅西
莱奥内尔・梅西           莱奥内尔・梅西
Cristiano Ronaldo       莱奥内尔・梅西
STEVE HARRIS            zinedine zidane
Cristiano Ronaldo       Gianluigi Buffon
我想添加一个值为1的列,如果这两列中的任何一列都有一个包含中文字符的字符串,则该列的值为1,如果两列都没有,则该列的值为0。函数如下所示:

def find_china_symbols(text):
    counter = 0
    if isinstance(text,str):
        for char in text:
            if ord(char) > 10000:
                counter += 1
        if counter > 0:
            return True
        else:
            return False
    else:
        return False
在此之前,我使用了
np.where
创建此列(如下所示),但在本例中不起作用。为什么不呢

df["Chinese"] = np.where(find_china_symbols(df["no1"]) | find_china_symbols(df["no2"]),1,0)
理想情况下,结果是:

no1                     no2                  Chinese
Paul Pogba              贝克汉姆              1
Gianluigi Buffon        莱奥内尔・梅西         1
莱奥内尔・梅西           莱奥内尔・梅西         1
Cristiano Ronaldo       莱奥内尔・梅西         1
STEVE HARRIS            zinedine zidane       0
Cristiano Ronaldo       Gianluigi Buffon      0

我会用
applymap

def find_china_symbols(text):
  return any(map(lambda char: ord(char) > 1000, text))

df['Chinese'] = df.applymap(find_china_symbols).any(1).astype(int)