我有两个不同列和大小的Dataframe.
第一个有一些列,其中一个是字符串字段(第1列).第二个数据帧有2列,一个是字符串字段(第4列),其中2个字用逗号分隔,另一个是整数字段(第5列).
我需要验证Dataframe 1中的第1列是否包含第4列Dataframe 2中的单词,并使用dataframe 2中的相应信息填充Dataframe 1.
例:
df1
column 1 column 2 column 3
0 bla bla sample1 bla bla sample2 a f
1 bla bla sample1 bla bla sample5 b g
2 bla bla sample3 bla bla sample4 c h
3 bla bla sample8 bla bla sample7 d i
4 bla bla sample1 bla bla sample2 e j
df2
column 4 column 5
0 ('sample1', 'sample2'), 50
1 ('sample3', 'sample4'), 35
2 ('sample1', 'sample5') 18
我需要输出:
Output:
df1
column 1 column 2 column 3 column 4 column 5
0 bla bla sample1 bla bla sample2 a f ('sample1', 'sample2') 50
1 bla bla sample1 bla bla sample5 b g ('sample1', 'sample5') 18
2 bla bla sample3 bla bla sample4 c h ('sample3', 'sample4') 35
3 bla bla sample8 bla bla sample7 d i NaN
4 bla bla sample1 bla bla sample2 e j ('sample1', 'sample2') 50
有任何想法吗?
谢谢!
最佳答案 我不保证这会特别快,但它完成了工作.我们将使用set logic来检查匹配.我们必须跳过一些箍,以便我们可以存储匹配元组的列表.我不认为这是一个特别好的主意.
import numpy as np
import pandas as pd
df1['setc'] = df1['column 1'].str.split().apply(set)
# Initialize so addition works
df1['column 4'] = [[] for i in range(len(df1))]
df1['column 5'] = 0
for idx, row in df2.iterrows():
m = (df1.setc.values & set(row['column 4'])) == set(row['column 4'])
df1.loc[m, 'column 4'] += pd.Series([[row['column 4']] for x in range(len(m))])[m]
df1.loc[m, 'column 5'] += row['column 5']
df1 = df1.drop(columns='setc')
# NaN where nothing matched
df1.loc[df1['column 4'].str.len().eq(0), ['column 4', 'column 5']] = np.NaN
输出:
column 1 column 2 column 3 column 4 column 5
0 bla bla sample1 sample5 sample2 a f [(sample1, sample2), (sample1, sample5)] 68.0
1 bla bla sample1 bla bla sample5 b g [(sample1, sample5)] 18.0
2 bla bla sample3 bla bla sample4 c h [(sample3, sample4)] 35.0
3 bla bla sample8 bla bla sample7 d i NaN NaN
4 bla bla sample1 bla bla sample2 e j [(sample1, sample2)] 50.0