data = {'COL1':['United States of America', 'United Kingdom'], 'COL2':['States of America', 'United States']}
df = pd.DataFrame(data)
df['IS_COL1_IN_COL2'] = df['COL1'].isin(df['COL1'])
display(df)
上面的代码给出下面的结果。我期望值是False,但是我会变成True?您能否让我知道我的代码有什么问题?谢谢。
答案 0 :(得分:1)
我认为您需要in
和lambda函数:
df['IS_COL1_IN_COL2'] = df.apply(lambda x:x['COL1'] in x['COL2'], axis=1)
df['IS_COL2_IN_COL1'] = df.apply(lambda x:x['COL2'] in x['COL1'], axis=1)
print (df)
COL1 COL2 IS_COL1_IN_COL2 \
0 United States of America States of America False
1 United Kingdom United States False
IS_COL2_IN_COL1
0 True
1 False
功能Series.isin
的工作原理不同-将COL1
的每个值与COL2
的所有值进行比较,检查更改的数据样本:
data = {'COL1':['United States of America', 'United Kingdom','USA','JAR'],
'COL2':['States of America', 'United States','UK', 'USA'],}
df = pd.DataFrame(data)
df['IS_COL1_IN_COL2_isin'] = df['COL1'].isin(df['COL2'])
df['IS_COL1_IN_COL2'] = df.apply(lambda x:x['COL1'] in x['COL2'], axis=1)
df['IS_COL2_IN_COL1'] = df.apply(lambda x:x['COL2'] in x['COL1'], axis=1)
print (df)
COL1 COL2 IS_COL1_IN_COL2_isin \
0 United States of America States of America False
1 United Kingdom United States False
2 USA UK True
3 JAR USA False
IS_COL1_IN_COL2 IS_COL2_IN_COL1
0 False True
1 False False
2 False False
3 False False