我想通過我的數據框中的列循環,如果該單詞存在,然後添加到一個新的列字。熊貓列表理解,如果語句
這是我的數據:
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.
就這樣,在輸出的第一行有2種水果在那裏,但我希望他們作爲獨立的行。 – jxn
啊,我沒有正確地閱讀這個問題。我不知道有沒有一種好的方法來平整不涉及複製行的數據框架,所以Asav的答案可能是要走的路 – lsankar4033