简体   繁体   English

在公共列中添加具有不同值的两个数据框

[英]Adding two data-frames with different values in a common column

I have two data frames.我有两个数据框。 I need to add them to calculate the total number of matches played and perform further calculations from the answer.我需要添加它们来计算比赛的总数并根据答案执行进一步的计算。 But every time I try to add them, some of the rows have Nan values.但是每次我尝试添加它们时,某些行都有 Nan 值。

Image of the two given dataframes两个给定数据帧的图像

The result of adding them comes out like this添加它们的结果是这样的

Image of the output on adding the two dataframes添加两个数据帧的输出图像

How do I add them without getting NaN values???如何在不获取 NaN 值的情况下添加它们???

sum after joining and grouping by teams:加入并按团队分组后的总和:

ipl=pd.concat([ipl17,ipl18]).groupby('Team').sum().reset_index()

print(ipl)

Output:输出:

   Team  Matches  Won  Lost  Tied  N/R  Points    NRR   For  Against
0   CSK       14    9     5     0    0      18  0.253  2488     2433
1    DD       28   11    17     0    0      22 -0.734  4516     4559
2    GL       14    4    10     0    0       8 -0.412  2406     2472
3   KKR       28   16    12     0    0      32  0.571  4692     4725
4  KXIP       28   13    15     0    0      26 -0.379  4417     4488
5    MI       28   16    12     0    0      32  1.101  4787     4524
6   RCB       28    9    18     0    1      19 -1.170  4167     4416
7   RPS       14    9     5     0    0      18  0.176  2180     2165
8    RR       14    7     7     0    0      14 -0.250  2130     2141
9   SRH       28   17    10     0    1      35  0.753  4451     4311

Explanation:解释:

Using concat join the two dataframe .使用concat连接两个dataframe using groupby('name') are grouped by Team .使用groupby('name')Team分组。 Subsequently, the sum is obtained for each team.随后,获得每个团队的总和。 Then reset_index is used to transform the index (Team) into columns .然后使用reset_indexindex (Team) 转换为columns If you prefer the latter you can skip it.如果你喜欢后者,你可以跳过它。

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

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