分组并填写缺少的日期时间值 [英] Group by and fill missing datetime values
本文介绍了分组并填写缺少的日期时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要尝试的是按合同和日期对Pandas Dataframe进行分组,并填写缺少的datetime值.
What I'm just trying is to group a Pandas Dataframe by contract and date, and fill missing datetime values.
我的输入是这样
contract datetime value1 value2
x 2019-01-01 00:00:00 50 60
x 2019-01-01 01:00:00 30 60
x 2019-01-01 02:00:00 70 80
y 2019-01-01 00:00:00 30 100
我想做的是为每个合约设置所有可能的日期时间(从00:00:00到23:00:00),并用NaN或None填充缺失值.
What I want to do is to have all possible datetimes (from 00:00:00 to 23:00:00) for each contract, and fill missing values with NaN or None.
非常感谢您.
推荐答案
您可以使用 DataFrame.groupby
和lambda函数:
You can use DataFrame.reindex
per groups with DataFrame.groupby
and lambda function:
df['datetime'] = pd.to_datetime(df['datetime'])
f= lambda x: x.reindex(pd.date_range(x.index.min().floor('d'),
.index.max().floor('d')+pd.Timedelta(23, 'H'),freq='H'))
df1 = (df.set_index('datetime')
.groupby('contract')
.apply(f)
.drop('contract', axis=1)
.reset_index())
print (df1)
这篇关于分组并填写缺少的日期时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文