简体   繁体   English

查找 ia strn 列在同一数据框中的列表列中,并创建具有值的第三列

[英]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.

相关问题 是否有一种SQL语法会根据同一表中第3列的相等值搜索2列来创建新列? - Is there a SQL syntax that will create new column by searching in 2 columns based on equal value of 3rd column in same table? 查找 2D 列表是否包含第三列不相关的值 - Find if a 2D list contains a value where the 3rd column is not relevant 创建将字典中的 2 个键映射到相同值的数据框列 - Create data frame column that maps 2 keys in a dictionary to same value pandas groupby,然后按第二列聚合,在第三列找到对应的值 - pandas groupby, then aggregate by a 2nd column and find corresponding value in a 3rd column Pandas:如果来自第三列的字符串值,则根据另一列的值创建列 - Pandas : Create columns based on values of another column if string value from 3rd column 有没有办法在数据框列和 str 列表中找到不同的值? - Is there a way to find the differing value(s) in a data frame column and str list? 从 pandas 列中查找非先前的非相同值,并将其作为 pandas 列分配给数据框 - Find non previous non-same value from pandas column and assign it to the data frame as a pandas column Pandas从列表中创建数据框列 - Pandas Create Data Frame Column from List 比较来自相同 pandas dataframe 的 2 列的值和基于比较的第 3 列的返回值 - comparing values of 2 columns from same pandas dataframe & returning value of 3rd column based on comparison xyzc值数组,找出第三列是否有c值等于1或不为零的行 - Array of xyzc values , Find out if 3rd column has any rows with c value equal to 1, or not zero
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM