python - 如何对 panda 数据框进行上采样

标签 python dataframe sampling do-loops

我有一个逗号分隔的数据文件,如下所示:

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/

相关文章:

image - 糟糕的 OpenCL ImageSampling 性能与 OpenGL TextureSampling

多输入法上的Python映射方法

python - 如何将 iterrows 下的功能转换为 pandas 中的一行

python - Pandas python : getting one value from a DataFrame

python - 根据单元格类型展开 pandas 列

python - 如何展平列中的层次索引

audio - 从音频输入捕获原始音频以在 Mac 上进行实时处理的最简单方法

python - sess.run() 多个操作 vs 多个 sess.run()

Python:关于 while 循环的非常基本的帮助

go - golang 中无需替换的示例