2014-01-25 69 views
1

起初,我不得不用DF與DatetimeIndex索引操作1列:大熊貓拆散不起作用

In [371]: dates 
2013-12-29 19:21:00 action1 
2013-12-29 19:21:01 action2 
2013-12-29 19:21:11 action1 
2013-12-29 19:21:13 action2 
          ... 
In [372]: dates.index 
    Out[372]: 
    <class 'pandas.tseries.index.DatetimeIndex'> 
    [2013-12-29 19:02:27, ..., 2014-01-13 16:30:31] 
    Length: 108957, Freq: None, Timezone: None 

我想繪製的VS一天某一類型的操作次數

所以我按分組行動迄今爲止,使用agg

grouped = dates.groupby([dates.index.to_period(freq = 'D'), 'actiontype']).agg(len) 

這給了我multiindexed系列:

... 
2014-01-13 action1  435 
      action2  2067 
.. 
2014-01-14 action1  455 
      action2  1007 
... 

這似乎正是我需要的。

但是當使用時unstack系列擺脫了多指標和繪製我的數據,並得到了錯誤:

In [379]: grouped.unstack() 

ValueError: freq not specified and cannot be inferred from first element 

什麼,這裏是我的錯誤?謝謝。

+0

我注意到有這個錯誤是一個[未決問題(https://github.com/pydata/pandas/issues/4342)。這與你在這裏看到的有關嗎? – chrisaycock

+0

@chrisaycock我已經看過這個頁面,但我不確定這是否是我的情況。熊貓我很新。 – Timofey

回答

1

如果你需要使用.unstack(),它不與多指標的工作,然後從非索引數據開始

index     mydate  action 
    0 2000-12-29 00:10:00 action1 
    1 2000-12-29 00:20:00 action2 
    2 2000-12-29 00:30:00 action2 
    3 2000-12-29 00:40:00 action1 
    4 2000-12-29 00:50:00 action1 
    5 2000-12-31 00:10:00 action1 
    6 2000-12-31 00:20:00 action2 
    7 2000-12-31 00:30:00 action2 

你可以做類似

df['day'] = df['mydate'].apply(lambda x: x.split()[0]) 
counts = df.groupby(['day', 'action']).agg(len) 

基本上你忘記關於日期時間是一個日期時間,你只要把它保存爲一個字符串,你只保留日期,放棄時間。現在大熊貓將在時間維度愚蠢的,但counts.unstack()給你

   mydate   
action  action1 action2 
day       
2000-12-29  3  2 
2000-12-31  1  2 
+0

謝謝!像魅力一樣工作。我還沒有來到actoss lambda,所以從來沒有通過這個解決方案。它只需要稍作修改 - 「mydate」應該被轉換爲字符串數據類型,以便「split()」。 :'dates ['day'] = dates ['mydate']。apply(lambda x:str(x).split()[0])' – Timofey