Reputation: 23
I need to convert a pandas dataframe with multiple rows into one row
col1. col2. col3. col4. col5.
0 1234 rule_1. '' '' ''
1 1234 '' rule_2. '' ''
2 2356 rule_1. '' '' ''
3 7890 '' '' rule_3 ''
4 1234 '' '' '' rule_4
I need to groupby by col1. and fill in the empty fields with the fields from the other rows.
col1. col2. col3. col4. col5
0 1234 rule_1. rule_2 '' rule_4
1 2356 rule_1. '' '' ''
3 7890 '' '' rule_3 ''
Upvotes: 2
Views: 200
Reputation: 323236
IIUC first mask the '' to nan , then we do groupby
+ first
s=df.mask(df=="''").groupby('col1.').first()
s # you can add reset_index()
col2. col3. col4. col5.
col1.
1234 rule_1. rule_2. NaN rule_4
2356 rule_1. NaN NaN NaN
7890 NaN NaN rule_3 NaN
Upvotes: 2