3 回答
TA贡献1796条经验 获得超7个赞
另一个解决方案:
m = pd.merge(df1, df2, on='Subject', how='outer')
m['maximum'] = m[['maximum_x', 'maximum_y']].sum(axis=1)
m['minimum'] = m[['minimum_x', 'minimum_y']].sum(axis=1)
print(m[['Subject', 'maximum', 'minimum']])
印刷:
Subject maximum minimum
0 Maths 200.0 90.0
1 Physics 196.0 94.0
2 Chem 184.0 115.0
3 language 88.0 54.0
TA贡献1982条经验 获得超2个赞
转换Subject
为DataFrames
by中的索引DataFrame.set_index
,然后使用DataFrame.add
with fill_value=0)
:
df = df_class_a.set_index('Subject').add(df_class_b.set_index('Subject'), fill_value=0)
print (df)
maximum minimum
Subject
Chem 184.0 115.0
Maths 200.0 90.0
Physics 196.0 94.0
language 88.0 54.0
concat
或者与聚合一起使用sum
:
df = pd.concat([df_class_a, df_class_b]).groupby('Subject', as_index=False).sum()
print (df)
Subject maximum minimum
0 Chem 184 115
1 Maths 200 90
2 Physics 196 94
3 language 88 54
TA贡献1757条经验 获得超7个赞
你可以试试:
df_add = pd.DataFrame({
'Subject': df_class_a['Subject'],
'Maximum': df_class_a['Maximum']+df_class_b['Maximum'],
'Minimum': df_class_a['Minimum']+df_class_b['Minimum']
})
添加回答
举报