我有两个数据帧(实际和目标),其标题如下:
print (df1)
WorkWeek Area Actual
0 202001 South 5
1 202001 North 5
2 202001 West 6
3 202001 East 8
4 202002 South 7
5 202002 North 9
6 202002 West 6
7 202002 East 3
8 202003 South 5
9 202003 North 85
10 202003 West 5
11 202003 East 11
12 202004 South 2
13 202004 North 2
14 202004 West 2
15 202004 East 2
print (df2)
WorkWeek South North West East
0 202001 60 90 70 80
1 202002 60 90 70 80
2 202003 60 90 70 80
3 202004 60 90 70 80
我想通过WW和Area加入df(Actual_vs_Targets)
如果我想添加更多区域,我应该怎么做? 谢谢!
答案 0 :(得分:6)
将DataFrame.melt
与DataFrame.merge
一起使用:
df22 = df2.melt('WorkWeek', var_name='Area', value_name='Target')
df = df1.merge(df22, on=['WorkWeek','Area'], how='left')
或将DataFrame.sem
与DataFrame.join
:
df22 = df2.set_index('WorkWeek').stack().rename_axis(['WorkWeek','Area']).rename('Target')
df = df1.join(df22, on=['WorkWeek','Area'])
print (df)
WorkWeek Area Actual Target
0 202001 South 5 60
1 202001 North 5 90
2 202001 West 6 70
3 202001 East 8 80
4 202002 South 7 60
5 202002 North 9 90
6 202002 West 6 70
7 202002 East 3 80
8 202003 South 5 60
9 202003 North 85 90
10 202003 West 5 70
11 202003 East 11 80
12 202004 South 2 60
13 202004 North 2 90
14 202004 West 2 70
15 202004 East 2 80