python - pandas 列表理解 if 语句

标签 python pandas list-comprehension

我想遍历数据框中的一列,如果该词存在,则将该词添加到新列中。

这是我的数据:

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/

相关文章:

访问类内部常量全局作用域变量的 Pythonic 方式?

Python 对象 __iter__ 没有被调用

python - 查找数据框集合中的所有重复列

python - 如何使用 Openpyxl 读取现有的工作表表格?

python - Pandas ,合并具有不同索引名称的两行

python - 如何计算 pandas 数据框中两个或多个非零元素之间的最小间隙?

python - 将一维 Numpy 数组作为行添加到 DataFrame

python - 在 Python 中将列表对转换为键值对

Python 类中的列表理解

Python - 在列表括号内调用的函数。它是如何工作的?