如何在不更改其他列的情况下重新采样一个“日期”列

问题描述 投票:0回答:1

我正在尝试重新采样我的DF中的一列而不改变另一列,所以这是所需的输出:

    Sensor_ID   Time_Instant    Measurement
0    14121    2013/11/14 00:00:00     0.8
1    14121    2013/11/14 03:00:00     0.6
2    14121    2013/11/14 06:00:00     0.4
3    14121    2013/11/14 09:00:00     0.4
4    14121    2013/11/14 12:00:00      0

这是我实际拥有的:

    Sensor_ID   Time_Instant    Measurement
0    14121    2013/11/14 17:00    0.8
1    14121    2013/11/14 18:00    0.6
2    14121    2013/11/14 19:00    0.4
3    14121    2013/11/14 20:00    0.4
4    14121    2013/11/14 21:00     0

这是我的尝试:

Mi_Meteo.columns = pd.to_datetime(Mi_Meteo.columns ,errors='coerce' ) 
Mi_Meteo.resample('3H', on='Time_Instant').sum()

但我得到这个错误:

'The grouper name Time_Instant is not found'

有什么建议吗? , 感谢你。

python-3.x pandas dataframe resampling
1个回答
0
投票

根据我的理解,你只想把时间拉上(缩减采样)而不影响其他值。如果是这样,你需要在最后填补新的时间。我用过NaNs。一旦长度相同,您可以替换原始列。

import numpy as np 

new = df.set_index('Time_Instant').resample('3H').count().index
df['Time_Instant'] = np.pad(new, (0,len(df) - len(new)), 'constant', 
                            constant_values=(0,np.nan))
© www.soinside.com 2019 - 2024. All rights reserved.