gpt4 book ai didi

python - pandas 列表理解 if 语句

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:38 24 4
gpt4 key购买 nike

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

这是我的数据:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com