我想遍历数据框中的一列,如果该词存在,则将该词添加到新列中。
这是我的数据:
import pandas as pd
d = {'title':pd.Series(['123','xyz']),
'question':pd.Series(["Hi i want to buy orange and pear", "How much is the banana?"])
}
df =pd.DataFrame(d)
df
question title
0 Hi i want to buy orange and pear 123
1 How much is the banana? xyz
代码:
#write to column if word exist:
fruit_list=['orange','pear','banana']
for i in fruit_list:
df['fruit']=[i if i in qn for qn in df['question']]
期望的输出:
question title fruit
0 Hi i want to buy orange and pear 123 orange
1 Hi i want to buy orange and pear 123 pear
2 How much is the banana? xyz banana
错误
SyntaxError: invalid syntax at the 'for' word.
最佳答案
我相信你想要的是:
fruit_list=['orange','pear','banana']
df['fruit'] = [[f for f in fruit_list if f in qn] for qn in df['question']]
关于python - pandas 列表理解 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757176/