按日记录事件类别和分组

时间:2014-03-20 09:29:21

标签: python logging pandas

我使用pandas处理一些日志。我基本上将其处理为以下时间序列:

time
2014-03-18 17:00:25.266462           rt/top_rt
2014-03-18 17:00:25.722639    follow/retweeted
2014-03-18 17:00:26.773057           rt/top_rt
2014-03-18 17:00:28.077047           rt/top_rt
2014-03-18 17:00:28.904139           rt/top_rt
2014-03-18 17:00:29.512671           rt/top_rt
2014-03-18 17:00:29.640878    follow/retweeted
2014-03-18 21:00:30.087161           rt/top_rt
2014-03-18 21:00:30.272342    follow/retweeted
2014-03-18 21:00:31.284734           rt/top_rt
2014-03-18 21:00:31.467828    follow/retweeted
2014-03-18 21:00:33.955612           rt/top_rt
2014-03-18 21:00:35.810813           rt/top_rt
2014-03-18 21:00:37.710910           rt/top_rt
2014-03-18 21:00:38.200717           rt/top_rt
...

我希望每天按日期类别和分组进行调整。所以我想得到一些看起来像的东西:

day           rt/top_rt   follow/retweeted  ...
2014-03-18    35          45
2014-03-19    67          90
2014-03-19    67          90
...

1 个答案:

答案 0 :(得分:1)

有几个选项(您可以使用df.pivotdf.pivot_tabledf.groupbydf.unstack进行此操作,但使用crosstab似乎很简单(这会默认计算频率http://pandas.pydata.org/pandas-docs/stable/generated/pandas.crosstab.html):

pd.crosstab(rows=dates, cols=log)

假设您有一个DataFrame df,其中包含DatetimeIndex和一列log,您可以这样做:

pd.crosstab(rows=df.index.date, cols=df['log'])

具体例子:

In [230]: s = """2014-03-18 17:00:25.266462,           rt/top_rt
     ...: 2014-03-18 17:00:25.722639,    follow/retweeted
     ...: 2014-03-18 17:00:26.773057,           rt/top_rt
     ...: 2014-03-18 17:00:28.077047,           rt/top_rt
     ...: 2014-03-18 17:00:28.904139,           rt/top_rt
     ...: 2014-03-18 17:00:29.512671,           rt/top_rt
     ...: 2014-03-18 17:00:29.640878,    follow/retweeted
     ...: 2014-03-18 21:00:30.087161,           rt/top_rt
     ...: 2014-03-18 21:00:30.272342,    follow/retweeted
     ...: 2014-03-18 21:00:31.284734,           rt/top_rt
     ...: 2014-03-18 21:00:31.467828,    follow/retweeted
     ...: 2014-03-19 21:00:33.955612,           rt/top_rt
     ...: 2014-03-19 21:00:35.810813,           rt/top_rt
     ...: 2014-03-19 21:00:37.710910,           rt/top_rt
     ...: 2014-03-19 21:00:38.200717,           rt/top_rt"""

In [231]: df = pd.read_csv(StringIO(s), sep=",", header=None, index_col=0, names=['time', 'log'], 
     ...:                  skipinitialspace=True, parse_dates=True)

In [232]: df
Out[232]: 
                                         log
time                                        
2014-03-18 17:00:25.266462         rt/top_rt
2014-03-18 17:00:25.722639  follow/retweeted
2014-03-18 17:00:26.773057         rt/top_rt
2014-03-18 17:00:28.077047         rt/top_rt
2014-03-18 17:00:28.904139         rt/top_rt
2014-03-18 17:00:29.512671         rt/top_rt
2014-03-18 17:00:29.640878  follow/retweeted
2014-03-18 21:00:30.087161         rt/top_rt
2014-03-18 21:00:30.272342  follow/retweeted
2014-03-18 21:00:31.284734         rt/top_rt
2014-03-18 21:00:31.467828  follow/retweeted
2014-03-19 21:00:33.955612         rt/top_rt
2014-03-19 21:00:35.810813         rt/top_rt
2014-03-19 21:00:37.710910         rt/top_rt
2014-03-19 21:00:38.200717         rt/top_rt

In [233]: pd.crosstab(df.index.date, df['log'])
Out[233]: 
log         follow/retweeted  rt/top_rt
row_0                                  
2014-03-18                 4          7
2014-03-19                 0          4