我在包含字符串的数据框中有两列。例如,
import pandas as pd
import numpy as np
data = [['Oct-2019', 'Oranges + Grapes + Pears', 'Grapes + Pears'],
['Nov-2019', 'Oranges + Grapes + Pears', 'Oranges + Grapes + Pears']]
df = pd.DataFrame(data, columns =['Date', 'Previous shopping list', 'Recent shopping list'])
print(df)
Fish = ['Salmon', 'Trout']
Fruit = ['Oranges', 'Grapes', 'Pears']
Date PSL RSL
0 Oct-2019 Oranges + Grapes Grapes + Pears
+ Pears + Salmon
1 Nov-2019 Oranges + Grapes Oranges + Grapes
+ Pears + Trout + Pears
我想比较两列中的字符串,并将文本输出到一个新列,该列说明两个列表之间发生了什么变化。例如,创建一个列,该列将检查与“水果”相关的字符串,并输出与上一个列表之前的购物列表相比,从最近的购物中删除了哪些水果。请参阅下面的所需输出:
Date PSL RSL Fruit lost Fish Lost
0 Oct-2019 Oranges + Grapes Grapes + Pears Oranges Salmon
+ Pears + Salmon
1 Nov-2019 Oranges + Grapes Oranges + Grapes Trout
+ Pears + Trout + Pears
在使用熊猫时我怎么能做到这一点!抱歉,如果这是第一次不清楚!
感谢您的任何建议/帮助!
哔哔one
扬帆大鱼
LEATH
相关分类