[英]Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value
I have this data frame with 2 columns, "Column A" and "Column B" , Column A is a string and column B is a list:我有这个包含 2 列的数据框, “Column A”和“Column B” ,A 列是一个字符串,B 列是一个列表:
A B c
cat | cat | elephant | gorilla | YES
dog | monkey | duck | giraffe | NO
bird | cow | bird | hamster | YES
and I want to evaluate if Column A is in Column B and if so to write YES or NO in this new column C我想评估A 列是否在 B 列中,如果是,则在这个新列 C 中写 YES 或 NO
I tried many ways, the very last one is:我尝试了很多方法,最后一个是:
df_epl["Marketo LSC"] = df_epl["Data Entry Point"].isin("Entry Point List")
but it gives me this error:但它给了我这个错误:
in isin在伊辛
raise TypeError(TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]
Try this尝试这个
import pandas as pd
df = pd.read_csv('res.csv') #Your csv here
C = []
for i in range(0,len(df)):
if df['A'][i] in df['B'][i]:
C.append('YES')
else:
C.append('NO')
df['C'] = C
print(df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.