Skip to content Skip to sidebar Skip to footer

Merge Rows Using Range In One Column And Math Operator On Other Columns

I know there are many threads on this df.groupby to merge rows with the same value in a column. But for the following situation, given this data frame: df = pd.DataFrame([{'timesta

Solution 1:

IIUC:

try:

df['timestamp']=pd.to_datetime(df['timestamp'])
#convert 'timestamp' column to datetime

Finally make use of groupby() and pd.Grouper():

out=df.groupby(pd.Grouper(key='timestamp',freq='1s')).max().reset_index()

OR

via assign(),floor() and groupby():

out=df.assign(timestamp=df['timestamp'].dt.floor('1s')).groupby('timestamp',as_index=False).max()

OR

via set_index() and resample():

out=df.set_index('timestamp').resample('1s').max().reset_index()

output of out:

timestampvalue1value202021-05-28 14:00:00     1233312021-05-28 14:00:01     71222021-05-28 14:00:02     423432021-05-28 14:00:03     241

Post a Comment for "Merge Rows Using Range In One Column And Math Operator On Other Columns"