Pandas 在自定义开始时间戳重新采样

标签 pandas

我正在尝试在给定的开始时间重新采样数据

my program:
sales = [{'Timestamp': '2018-06-22 15:15:00', 'Jan': 150, 'Feb': 200, 'Mar': 140},
        {'Timestamp': '2018-06-22 15:44:00',  'Jan': 250, 'Feb': 250, 'Mar': 250},
        {'Timestamp': '2018-06-22 15:46:00',  'Jan': 200, 'Feb': 210, 'Mar': 215},
        {'Timestamp': '2018-06-22 16:16:00',  'Jan': 200, 'Feb': 210, 'Mar': 215},
        {'Timestamp': '2018-06-22 16:18:00',  'Jan': 200, 'Feb': 210, 'Mar': 215},
        {'Timestamp': '2018-06-22 16:20:00',  'Jan': 50,  'Feb': 90,  'Mar': 95 }]
df = pd.DataFrame(sales)
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df.set_index('Timestamp')

ResampledDF = pd.DataFrame()
ResampledDF['J'] = df.Jan.resample("30T").max()
ResampledDF['F'] = df.Feb.resample("30T").max()
ResampledDF['M'] = df.Mar.resample("30T").max()
print(ResampledDF)

输出:
                       J    F    M
Timestamp                         
2018-06-22 15:00:00  150  200  140
2018-06-22 15:30:00  250  250  250
2018-06-22 16:00:00  200  210  215

这里的输出从 15:00:00 开始自动采样数据,而我希望第一行在 15:15:00,第二行在 15:45:00 等,如下所示

所需输出:
                        J    F    M
 Timestamp                         
 2018-06-22 15:15:00  250  250  250
 2018-06-22 15:45:00  200  210  215
 2018-06-22 16:15:00  200  210  215

最佳答案

使用 base parameter :

In [233]: df.resample('30T', base=15).max()
Out[233]:
                     Feb  Jan  Mar
Timestamp
2018-06-22 15:15:00  250  250  250
2018-06-22 15:45:00  210  200  215
2018-06-22 16:15:00  210  200  215

关于Pandas 在自定义开始时间戳重新采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50987802/

相关文章:

python - 在 Python 中获取从周日开始的周数的问题?

python - 删除 pandas df 中的列表字符串开头

python - 将 .csv 行从引号中解开

python - 如何在 pandas 多重索引中按小时分组

python - pandas:两列的条件滚动和

python - pandas 读取 sql db2 损坏十进制

python - 根据不同的列值分配唯一值

python - 在 Python 中二值化 float64 Pandas Dataframe

python - Pandas 按 Zscore 过滤每组异常

python - 使用Python的geopy + Google Maps Geocoding API的HTTP错误500