简体   繁体   中英

merging rows to a single row in pandas based on timestamp

I have a pandas dataframe which looks like

Time       C1    C2
08:00:20   5     nan
08:00:40   nan   6
08:05:12   10    nan
08:05:32   nan   14  

I want to merge rows closely related in time to a single row and remove nulls from my dataframe, some thing like this

Time    C1    C2
08:00   5     6
08:05   10    14

Can someone help me with how to merge rows to get such outputs

If Time is string use indexing by first 5 values and pass to GroupBy.first :

df1 = df.groupby(df.pop('Time').str[:5]).first().reset_index()
print (df1)
    Time    C1    C2
0  08:00   5.0   6.0
1  08:05  10.0  14.0

If datetime in column use Series.dt.strftime :

df['Time'] = pd.to_datetime(df['Time'])
df1 = df.groupby(df.pop('Time').dt.strftime('%H:%M')).first().reset_index()
print (df1)
    Time    C1    C2
0  08:00   5.0   6.0
1  08:05  10.0  14.0

Using bfill and drop_duplicates :

df['Time'] = pd.to_datetime(df['Time']).dt.strftime('%H:%M')
df[['C1', 'C2']] = df.groupby('Time').bfill()
df.drop_duplicates('Time')

    Time    C1    C2
0  08:00   5.0   6.0
2  08:05  10.0  14.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM