-
芜湖不芜
另一个解决方案: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 minimum0 Maths 200.0 90.01 Physics 196.0 94.02 Chem 184.0 115.03 language 88.0 54.0
-
临摹微笑
转换Subject为DataFramesby中的索引DataFrame.set_index,然后使用DataFrame.addwith fill_value=0):df = df_class_a.set_index('Subject').add(df_class_b.set_index('Subject'), fill_value=0)print (df) maximum minimumSubject Chem 184.0 115.0Maths 200.0 90.0Physics 196.0 94.0language 88.0 54.0concat或者与聚合一起使用sum:df = pd.concat([df_class_a, df_class_b]).groupby('Subject', as_index=False).sum()print (df) Subject maximum minimum0 Chem 184 1151 Maths 200 902 Physics 196 943 language 88 54
-
长风秋雁
你可以试试: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']})