Skip to content Skip to sidebar Skip to footer

Combine Similar Rows To One Row In Python Dataframe

I have some dataframe as below, what I want to do is to combine the rows with same 'yyyymmdd' and 'hr ' into one row. (there are several rows with same 'yyyymmdd' and 'hr' )

Solution 1:

Put your data into a Pandas dataframe, and then groupby and get the max of each group, Copy-Pasting your example into a csv, it looks like this:

import pandas as pd
df = pd.read_csv('df.csv',index_col=0)
df_combined = df.groupby(['yyyymmdd','hr']).max()
df_combined

Output:

enter image description here

Use reset_index() in case you don't want the multi-index.

Post a Comment for "Combine Similar Rows To One Row In Python Dataframe"