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 ....
I'm trying to Group by
You can use the groupby
to post process however you like
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.