Plot number of occurrences from Pandas DataFrame

后端 未结 2 809
自闭症患者
自闭症患者 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:27

    You can get the counts by using

    df.groupby([df.index.date, 'action']).count()
    

    or you can plot directly using this method

    df.groupby([df.index.date, 'action']).count().plot(kind='bar')
    

    You could also just store the results to count and then plot it separately. This is assuming that your index is already in datetimeindex format, otherwise follow the directions of @mkln above.

    0 讨论(0)
  • 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'])
    
    0 讨论(0)
提交回复
热议问题