Pandas 按日期分組和計數(shù)。然后將計數(shù)轉(zhuǎn)置為列名稱
我有這個數(shù)據(jù)框import pandas as pdfrom datetime import datetimedf = pd.DataFrame([ {"_id": "1", "date": datetime.strptime("2020-09-29 07:00:00", '%Y-%m-%d %H:%M:%S'), "status": "started"}, {"_id": "2", "date": datetime.strptime("2020-09-29 14:00:00", '%Y-%m-%d %H:%M:%S'), "status": "end"}, {"_id": "3", "date": datetime.strptime("2020-09-25 17:00:00", '%Y-%m-%d %H:%M:%S'), "status": "started"}, {"_id": "4", "date": datetime.strptime("2020-09-17 09:00:00", '%Y-%m-%d %H:%M:%S'), "status": "end"}, {"_id": "5", "date": datetime.strptime("2020-09-19 07:00:00", '%Y-%m-%d %H:%M:%S'), "status": "end"}, {"_id": "6", "date": datetime.strptime("2020-09-19 08:00:00", '%Y-%m-%d %H:%M:%S'), "status": "end"},]).set_index('date')看起來像這樣: _id statusdate 2020-09-29 07:00:00 1 started2020-09-29 14:00:00 2 end2020-09-25 17:00:00 3 started2020-09-17 09:00:00 4 end2020-09-19 07:00:00 5 end我正在嘗試按天分組并計算每個狀態(tài)。但我想在列名稱中包含名稱的名稱。這是所需的輸出: status_started status_enddate2020-09-29 07:00:00 1 12020-09-25 17:00:00 1 02020-09-17 09:00:00 0 12020-09-19 07:00:00 0 2我試過這個:df = df.groupby([pd.Grouper(freq='d'), 'status']).agg({'status': "count"})df = df.reset_index(level="status")out: statusdate status 2020-09-17 end 12020-09-19 end 22020-09-25 started 12020-09-29 end 12020-09-29 started 1但并沒有成功改造df。
查看完整描述