我有一个逗号分隔的数据文件,如下所示:
ID | StartTimeStamp | EndTimeStamp | Duration (in seconds) | AssetName
1233 | 2017-01-01 00:00:02 | 2017-01-01 00:10:01 | 601 | Car1
1233 | 2017-01-01 00:10:01 | 2017-01-01 00:10:12 | 11 | Car1
...
1235 | 2017-01-01 00:00:02 | 2017-01-01 00:10:01 | 601 | CarN
等等
现在我想使用开始时间和持续时间来创建以下内容来对数据进行上采样。
ID | StartTimeStamp | AssetName
1233 | 2017-01-01 00:00:02 | Car1
1233 | 2017-01-01 00:00:03 | Car1
1233 | 2017-01-01 00:00:04 | Car1
...
1233 | 017-01-01 00:10:01 | Car1
...
1235 | 2017-01-01 00:00:02 | CarN
1235 | 2017-01-01 00:00:03 | CarN
1235 | 2017-01-01 00:00:04 | CarN
... (i.e. 601 rows of data one per second)
1235 | 2017-01-01 00:10:01 | CarN
但我对如何做到这一点增加了可能性,因为上采样似乎只能与时间序列一起使用?我正在考虑使用 StartTimeStamp
和文件中的秒数来使用 for 循环,但我不知道如何进行此操作?
最佳答案
您可以对每个 ID
组重新采样,然后填充字符列中的空白
import pandas as pd
df_resampled = df.set_index(pd.to_datetime(df.StartTimeStamp)).groupby('ID')
# Expand out the dataframe for one second
df_resampled = df_resampled.resample('1S').asfreq()
# Interpolate AssetName for each group
df_resampled['AssetName'] = df_resampled['AssetName'].ffill().bfill()
关于python - 如何对 panda 数据框进行上采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48069382/