有了这两个简化的数据框
df1=pd.DataFrame({'COUNTRY':['A','A','A','B','B','C','C','C'],'YEAR':[1,2,3,1,2,1,2,3],'VALUE':[100,100,100,100,100,100,100,100]})
df2=pd.DataFrame({'COUNTRY':['A','A','B','B','C'],'YEAR':[1,3,1,2,3],'PROPORTION':[0.5,0.1,0.5,0.2,0.1]})
df1
COUNTRY YEAR VALUE
0 A 1 100
1 A 2 100
2 A 3 100
3 B 1 100
4 B 2 100
5 C 1 100
6 C 2 100
7 C 3 100
df2
COUNTRY YEAR PROPORTION
0 A 1 0.5
1 A 3 0.1
2 B 1 0.5
3 B 2 0.2
4 C 3 0.1
如何将df1.VALUE
与df2.PROPORTION
和df1.COUNTRY=df2.COUNTRY
匹配的df1.YEAR=df2.YEAR
乘以
VALUE=[50,100,10,50,20,100,100,10]
答案 0 :(得分:2)
执行此操作的另一种方法是将pandas内部数据与索引对齐。
将set_index
和mul
与fill_value=1
一起使用。
df1i = df1.set_index(['COUNTRY','YEAR'])
df2i = df2.set_index(['COUNTRY','YEAR'])
df2i['PROPORTION'].mul(df1i['VALUE'], fill_value=1).rename('PROPORTION').reset_index()
输出:
COUNTRY YEAR PROPORTION
0 A 1 50.0
1 A 2 100.0
2 A 3 10.0
3 B 1 50.0
4 B 2 20.0
5 C 1 100.0
6 C 2 100.0
7 C 3 10.0
答案 1 :(得分:1)
您可以先按merge
然后按mul
df1['New Value']=df1.merge(df2,how='left').PROPORTION.mul(df1.VALUE)
答案 2 :(得分:0)
尝试一下:
df1=pd.DataFrame({'COUNTRY':['A','A','A','B','B','C','C','C'],'YEAR':[1,2,3,1,2,1,2,3],'VALUE':[100,100,100,100,100,100,100,100]})
df2=pd.DataFrame({'COUNTRY':['A','A','B','B','C'],'YEAR':[1,3,1,2,3],'PROPORTION':[0.5,0.1,0.5,0.2,0.1]})
df = df1.merge(df2, on=['COUNTRY', 'YEAR'], how='left').fillna(1)
df['res'] = df['VALUE']*df['PROPORTION']
df
输出:
COUNTRY YEAR VALUE PROPORTION res
0 A 1 100 0.5 50.0
1 A 2 100 1.0 100.0
2 A 3 100 0.1 10.0
3 B 1 100 0.5 50.0
4 B 2 100 0.2 20.0
5 C 1 100 1.0 100.0
6 C 2 100 1.0 100.0
7 C 3 100 0.1 10.0
答案 3 :(得分:0)
df1['VALUE']=df1.merge(df2,how='left').fillna(1)['PROPORTION'].mul(df1['VALUE'])