-1

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.

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???

  • 2
    [please don't put images of code or data in the question, instead post them as text](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) so people can reproduce your question – anky Aug 31 '19 at 14:37
  • I am sorry about that. I didn't know how to format it so it was readable as code. – sahil_mmgn Sep 01 '19 at 07:11
  • reading [how to produce good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) would help you – anky Sep 01 '19 at 07:12

1 Answers1

0

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. using groupby('name') are grouped by Team. Subsequently, the sum is obtained for each team. Then reset_index is used to transform the index (Team) into columns. If you prefer the latter you can skip it.

ansev
  • 30,322
  • 5
  • 17
  • 31