简体   繁体   中英

How often does a problem occur per day and location?

I have dataframe like this:

Date                  Location_ID   Problem_ID  
---------------------+------------+----------  
2013-01-02 10:00:00  | 1          |  43  
2012-08-09 23:03:01  | 5          |  2  
...

How can I count how often a Problem occurs per day and per Location?

Use groupby with converting Date column to date s or Grouper with aggregate size :

print (df)
                  Date  Location_ID  Problem_ID
0  2013-01-02 10:00:00            1          43
1  2012-08-09 23:03:01            5           2

#if necessary convert column to datetimes 
df['Date'] = pd.to_datetime(df['Date'])

df1 = df.groupby([df['Date'].dt.date, 'Location_ID']).size().reset_index(name='count')
print (df1)
         Date  Location_ID  count
0  2012-08-09            5      1
1  2013-01-02            1      1

Or:

df1 = (df.groupby([pd.Grouper(key='Date', freq='D'), 'Location_ID'])
         .size()
         .reset_index(name='count'))

If first column is index:

print (df)
                     Location_ID  Problem_ID
Date                                        
2013-01-02 10:00:00            1          43
2012-08-09 23:03:01            5           2


df.index = pd.to_datetime(df.index)

df1 = (df.groupby([df.index.date, 'Location_ID'])
        .size()
        .reset_index(name='count')
        .rename(columns={'level_0':'Date'}))
print (df1)
         Date  Location_ID  count
0  2012-08-09            5      1
1  2013-01-02            1      1

df1 = (df.groupby([pd.Grouper(level='Date', freq='D'), 'Location_ID'])
         .size()
         .reset_index(name='count'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM