I have a single dataframe like this:
df1 = pd.DataFrame([{'GameID': 15, 'Column1': 20,'Column2': 25, 'Column3': -15,'Column4': '','Column5': ''}, {'GameID': 15, 'Column1': '','Column2': '','Column3': '','Column4': 30,'Column5': 40}])
The output is like this:
GameID Column1 Column2 Column3 Column4 Column5
0 15 20 25 -15
1 15 30 40
What I'm tring to do here is to merge my row, meaning that the output will be like
GameID Column1 Column2 Column3 Column4 Column5
0 15 20 25 -15 30 40
I've tried to groupby but the result isn't the one expected
df1 = pd.DataFrame(df1.groupby('GameID'))
0 1
0 15 GameID Column1 Column2 Column3 Column4 Colu...
Any suggestion on this topic will be much appreciated
Thanks
Geoffrey
This is another alternative without groupby()
as greatly suggested in the comments, using np.nan
and fillna()
:
df1 = df1.replace({'':np.nan}).fillna(method='bfill').head(1)
Output:
GameID Column1 Column2 Column3 Column4 Column5
0 15 20.0 25.0 -15.0 30.0 40.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.