如何对熊猫数据框进行上采样

时间:2018-01-02 23:45:24

标签: python dataframe sampling do-loops

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

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

但我增加了如何做到这一点的几率,因为上采样似乎只能与时间序列一起工作?我正在考虑使用import { setContext } from 'apollo-link-context'; const httpLink = new HttpLink({ uri: 'http://localhost:4000/'}); const authHeader = setContext( request => new Promise((success, fail) => { getSessionToken().then(token => success({ headers: { authorization: `Bearer ${token}` }})) }) ) const client = new ApolloClient({ link: concat(authHeader, httpLink), cache: new InMemoryCache(), }) 和文件中的秒数来使用for循环,但是如何解决这个问题却不知所措?

1 个答案:

答案 0 :(得分:2)

您可以为每个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()