Combining Rows In Pandas Dataframe By Comparing Multiple Columns
How can I achieve the expected result from the following DataFrame df col_1 col_2 col_3 col_4 0 Non-Saved www.google.com POST 20,5
Solution 1:
If the previous solution worked, then let's try this one:
l = lambda x: ' , '.join(x.unique())
df = df.apply(lambda x: x.str.strip()).replace('',np.nan)
print(df.groupby(df.col_1.ffill())\
.agg({'col_2': l,'col_3': l, 'col_4':'first'})\
.reset_index())
Output:
col_1 col_2 \
0 Non-Saved www.google.com , www.facebook.com , www.linked...
1 Saved www.Quora.com , www.gmail.com
col_3 col_4
0 POST , GET , OTHER 20,567
1 POST , GET 6,337
Post a Comment for "Combining Rows In Pandas Dataframe By Comparing Multiple Columns"