1 回答
TA贡献1865条经验 获得超7个赞
df1= df.groupby(['Name','Experiment Start','Food']).size().unstack()
df1['All'] = df1.sum(axis=1)
或者crosstab:
df1 = pd.crosstab([df['Name'],df['Experiment Start']], df['Food'], margins=True)
df = df.sort_values(by='Time', ascending=False)
df = df.drop_duplicates(subset=['Name', 'Experiment Start'])
df['Last Meal in Experiment'] = df['Experiment Start'].dt.floor('D') + df['Time']
df = df.join(df1, on=['Name','Experiment Start'])
print (df)
Name Time Food Experiment Start Last Meal in Experiment \
Index
6 Peter 15:00:00 Nut 2018-08-06 07:30:00 2018-08-06 15:00:00
9 Peter 13:10:00 Nut 2018-08-12 10:30:00 2018-08-12 13:10:00
Cheese Nut Count
Index
6 2 5 7
9 1 2 3
添加回答
举报