Skip to main content
 首页 » 编程设计

python之使用 Pandas 从规范化的 csv 中提取下采样时间序列

2024年10月25日12jpfss

我有一个来自 CSV 的数据框,看起来像这样(此处的示例数据:http://www.speedyshare.com/9A2zf/download/sample.csv):

                          event    name          user  count  amount  commission 
2011-05-23 00:00:00  2011-07-22  normal  reading_arts      2      26         0.0 
2011-05-23 00:00:00  2011-07-23  normal  reading_arts     14     182         0.0 
2011-05-24 00:00:00  2011-07-22  normal  reading_arts      4      52         0.0 
2011-05-24 00:00:00  2011-07-22  normal  reading_arts      3      39         0.0 
2011-05-26 00:00:00  2011-07-23  normal  reading_arts      2      30         0.0 
2011-05-26 00:00:00  2011-07-23  normal  reading_arts      5      75         0.0 
2011-05-26 00:00:00  2011-07-22  normal  reading_arts      1      13         0.0 
2011-05-27 15:39:28  2011-07-23  normal       hickies     16     208       -10.4 
2011-06-01 00:00:00  2011-07-23  normal  reading_arts      2      30         0.0 
2011-06-02 00:00:00  2011-07-23  normal  reading_arts     17     221         0.0 

..我创建的:

data = read_csv('2011.csv',  
                names=('event', 'user', 'count', 'amount', 'commission'),  
                parse_dates=True) 

“事件”虽然看起来像日期,但实际上只是特定事件的标识符。

您会注意到 DateTimeIndex 中有重复的条目,例如:2011-05-23 00:00:00。

我最终想要的是每个事件的每个用户的一组 3 个时间序列(针对计数、金额和佣金中的每一个),通过求和向下采样到每周桶。我还想为每个事件创建类似的时间序列,这只是每个用户每个事件时间序列的总和。

我该怎么做?

请您参考如下方法:

编辑 - 试试这段代码:

注意 - 我获取了 csv 并为每一行添加了一个标题。我添加的第 1 行列标题是:

time    event   name    user    count   amount  commission 

尝试运行它,如果它仍然不是您要找的,请告诉我。

import pandas as pd 
import numpy as np 
 
df= pd.DataFrame.from_csv('sample.csv') 
 
resamp = df.groupby(['event','user']).resample('W', how='sum')