-3

I'm trying to Group by a dataframe based on columns.

        name         month1      month2         month3       .....    month n
    0     john         50         NaN             NaN        .....      .
    1     john         NaN         25             NaN        .....      .
    2     john         NaN        NaN             10         .....      .
    3     mark         NaN         0              NaN        .....      .
    4     mark         15         NaN             NaN        .....      .
    5     paul         NaN        NaN             100        .....      .

Final output

        name         month1      month2         month3     ....
    0     john         50           25             10      ....
    1     mark         15           0              NaN     ....
    2     paul         NaN          NaN            100     ....
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • Does this answer your question? [Pandas group-by and sum](https://stackoverflow.com/questions/39922986/pandas-group-by-and-sum) – Björn Jul 22 '21 at 08:26
  • Does this answer your question? [Pandas dataframe get first row of each group](https://stackoverflow.com/questions/20067636/pandas-dataframe-get-first-row-of-each-group) – Anurag Dabas Jul 22 '21 at 08:46

2 Answers2

0

I'm trying to Group by

you said it yourself

You can use the groupby to post process however you like

Dr. Prof. Patrick
  • 1,280
  • 2
  • 15
  • 27
0

Group the dataframe by name column then call first, and finally reset the index:

>>> df.groupby('name').first().reset_index()

   name  month1  month2  month3
0  john    50.0    25.0    10.0
1  mark    15.0     0.0     NaN
2  paul     NaN     NaN   100.0
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45