我想比较两个数据框并输出一个具有差异的数据框。但是,我可以忍受2天之内的日期差异,并且在5分之内获得分数。如果值在可接受范围内,我将保留df1中的值。
df1
id group date score
10 A 2020-01-10 50
29 B 2020-01-01 80
39 C 2020-01-21 84
38 A 2020-02-02 29
df2
id group date score
10 B 2020-01-11 56
29 B 2020-01-01 81
39 C 2020-01-22 85
38 A 2020-02-12 29
我的预期输出:
id group date score
10 A -> B 2020-01-10 50 -> 56
29 B 2020-01-01 80
39 C 2020-01-21 84
38 A 2020-02-02 -> 2020-02-12 29
因此,我想按单元格和某些列的条件比较数据框。
我从这里开始:
df1.set_index('id', inplace=True)
df2.set_index('id', inplace=True)
result = []
for col in df1.columns:
for index, row in df1.iterrows():
diff = []
compare_item = row[col][index]
for index, row in df2.iterrows():
if col == 'date':
# acceptable if it's within 2 days differences
if col == 'score':
# acceptable if it's within 5 points differences
if compare_item == row[col][index]:
diff.append(compare_item)
else:
diff.append('{} --> {}'.format(compare_item, row[col]))
result.append(diff)
df = pd.DataFrame(result, columns = [df1.columns])
答案 0 :(得分:0)
让我们尝试一下:
thresh = {'date':pd.to_timedelta('2D'),
'score':5}
def update(col):
name = col.name
# if there is a threshold, we update only if threshold is surpassed
if name in thresh:
return col.where(col.sub(df2[name]).abs()<=thresh[name], df2[name])
# there is no threshold for the column
# return the corresponding column from df2
return df2[name]
df1.apply(update)
输出:
group date score
id
10 B 2020-01-10 56
29 B 2020-01-01 80
39 C 2020-01-21 84
38 A 2020-02-12 29