Plot number of occurrences from Pandas DataFrame

后端 未结 2 817
自闭症患者
自闭症患者 2021-01-02 04:53

I have a DataFrame with two columns. One of them is containing timestamps and another one - id of some action. Something like that:

2000-12-29 00:10:00     a         


        
2条回答
  •  渐次进展
    2021-01-02 05:33

    Starting from

                    mydate col_name
    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
    

    You can do

    df['mydate'] = pd.to_datetime(df['mydate'])
    df = df.set_index('mydate')
    df['day'] = df.index.date
    counts = df.groupby(['day', 'col_name']).agg(len)
    

    but perhaps there's an even more straightforward way. the above should work anyway.

    If you want to use counts as a DataFrame, I'd then transform it back

    counts = pd.DataFrame(counts, columns=['count'])
    

提交回复
热议问题