[英]how to check if a list of column in not present in a dataframe pandas
[英]How to do a keyword search and list the keywords present in selected column to a new column for a pandas dataframe?
我有一個 df 列 foodhist
它可以包括'cheeseburgers,largepizzafrompizzahut'
作為輸入
df['foodhist']
我有一個清單unhealthyfoods = ['burger','pizza' etc.]
我想添加df['unhealthyfoodyouhad']
列,它將在df['foodhist']
中查找unhealthyfoods
中的所有項目並將它們添加到此列。
df['foodhist']
中的數據不能間隔,不能分開,但我仍然想尋找關鍵字的外觀並將它們返回df['unhealthyfoodyouhad']
。
為此,在 'cheeseburgers,largepizzafrompizzahut' 中應在df['unhealthyfoodyouhad']
列中添加 burger,pizza。
嘗試這個:
import re
unhealthyfoods=['burger','pizza']
df=pd.DataFrame({'foodhist':['cheeseburger','burgerandlargepizza']})
list1=[]
for food in unhealthyfoods:
list1.append([re.findall(food,x) for x in df['foodhist']])
df['unhealthyfoodyouhad']=pd.DataFrame(list1).T.sum(1)
df['unhealthyfoodyouhad']=[','.join(i) for i in df['unhealthyfoodyouhad']]
output:
df
Out[95]:
foodhist unhealthyfoodyouhad
0 cheeseburger burger
1 burgerandlargepizza burger,pizza
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.