繁体   English   中英

组合两个数据框,将值与相应的名称相加

[英]combine two dataframe adding up the values to the corresponding names

下面的代码怎么做?

数据框 1

             genres1
Drama         1168
Adventure     517
Documentary   431
Animation     353
Crime         335
Romance       328
Sci-Fi        20

数据框 2

            genres2
Comedy       1625
Action       1233
Drama        1200
Documentary  688
Crime        562
Adventure    390
Animation    336

我的输出可以在一个新列上完全添加值吗,像这样

              Genre
Drama         2368
Documentary   1119
Adventure     907
Animation     689
Crime         897
Romance       328
Sci-Fi        20

尝试 :

df = pd.concat([df1,df2]).fillna(0)
df['Genere'] = df['genres1'] + df['genres2']
df = df.groupby(df.index).sum().drop(['genres1','genres2'],axis=1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM