0

I've been trying to do some DataFrame manipulation but I can't find how to do this. I've a DataFrame like this :

Iso-2  FAOSTAT  Production %   Solde %
0     CN       41      0.247561       NaN
10    US      231      0.162421       NaN
14    AR        9      0.012170       NaN
21    GR       84      0.012737       NaN
0     CN       41           NaN  0.321039
6     ID      101           NaN  0.032065
7     UZ      235           NaN  0.031848
8     MX      138           NaN  0.022452

I want to gather the values of the third and fourth column in the same row where the first and second are the same. I'd like to have something like this :

Iso-2  FAOSTAT  Production %   Solde %
0     CN       41      0.247561  0.321039
10    US      231      0.162421       NaN
14    AR        9      0.012170       NaN
21    GR       84      0.012737       NaN
6     ID      101           NaN  0.032065
7     UZ      235           NaN  0.031848
8     MX      138           NaN  0.022452

Any help would be greatly appreciated, and sorry if the post isn't the best you have ever looked it's my first one.

hhvni
  • 1
  • 1
  • Looks like : `df.groupby(level=0,sort=False).first()` – anky Apr 14 '21 at 12:25
  • Or use `df.groupby('Iso-2', sort=False, as_index=False).first()` – jezrael Apr 14 '21 at 12:25
  • @jezrael thank you, your line just gave me what I was looking for ! – hhvni Apr 14 '21 at 12:30
  • May I ask why are we using .first() here ? Tbh I dont fully understand why we should do like this. Firstly, I thougut ```df.groupby('Iso-2', as_index = False)```was enough – hhvni Apr 14 '21 at 12:34
  • because ouput from `df.groupby('Iso-2', as_index = False)` is only groupby object, need chain some aggregate function , here `first` – jezrael Apr 14 '21 at 12:35

0 Answers0