简体   繁体   中英

Transforming data frame (row to column and count)

Sorry for the dumb question, but I got stuck. I have the dataframe with the next structure:

|.....| ID | Cause |         Date       |  
|  1  | AR | SGNLss| 10-05-2019 05:01:00|  
|  2  | TD | PTRXX | 12-05-2019 12:15:00|
|  3  | GZ | FAIL  | 10-05-2019 05:01:00|  
|  4  | AR | PTRXX | 12-05-2019 12:15:00|  
|  5  | GZ | SGNLss| 10-05-2019 05:01:00|
|  6  | AR | FAIL  | 10-05-2019 05:01:00|  

What I want is convert DATE column value to columns rounded to day so that the expected DF will have ID, 10-05-2019, 11-05-2019, 12-05-2019... columns and the values - the number of events (Causes) happened on this Id.

It's not a problem to round day and count values separately, but I can't get how to do both these operations.

You can use pd.crosstab :

pd.crosstab(df['ID'], df['Date'].dt.date)

Output:

Date  2019-10-05  2019-12-05
ID                          
AR             2           1
GZ             2           0
TD             0           1

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