[英]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
:如果
Time
是字符串,则使用前 5 个值索引并传递给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
:如果列中的
datetime
时间使用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
:使用
bfill
和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.