【发布时间】:2022-01-11 16:06:30
【问题描述】:
我正在尝试合并以下两个数据框,但没有得到预期的结果。
import pandas as pd
previous_dict = [{"category1":"Home", "category2":"Power","usage":"15","amount":"65"},
{"category1":"Home", "category2":"Power","usage":"2","amount":"15"},
{"category1":"Home", "category2":"Vehicle","usage":"6","amount":"5"}
]
current_dict = [{"category1":"Home", "category2":"Power","usage":"16","amount":"79"},
{"category1":"Home", "category2":"Power","usage":"0.5","amount":"2"},
{"category1":"Home", "category2":"Vehicle","usage":"3","amount":"4"}
]
df_previous = pd.DataFrame.from_dict(previous_dict)
print(df_previous)
df_current = pd.DataFrame.from_dict(current_dict)
print(df_current)
df_merge = pd.merge(df_previous, df_current, on=['category1','category2'], how='outer',indicator=True, suffixes=('', '_y'))
print(df_merge)
上一年的数据框
category1 category2 usage amount
0 Home Power 15 65
1 Home Power 2 15
2 Home Vehicle 6 5
当前年份数据框
category1 category2 usage amount
0 Home Power 16 79
1 Home Power 0.5 2
2 Home Vehicle 3 4
当前结果:
category1 category2 usage amount usage_y amount_y _merge
0 Home Power 15 65 16 79 both
1 Home Power 15 65 0.5 2 both
2 Home Power 2 15 16 79 both
3 Home Power 2 15 0.5 2 both
4 Home Vehicle 6 5 3 4 both
但我的预期结果是,
category1 category2 usage amount usage_y amount_y _merge
0 Home Power 15 65 16 79 both
3 Home Power 2 15 0.5 2 both
4 Home Vehicle 6 5 3 4 both
当类别 1 和类别 2 在两个表中多次具有相同的值时,我只想将其与正确的顺序匹配。我怎样才能得到我期望的值?
【问题讨论】:
-
您似乎想将一个数据框的
usage和amount列插入到另一个数据框。你能更好地解释合并逻辑吗?