我想用聚合功能count
按日期和小时分组,并在输出中为每个不同的ID(在列中)拆分结果。
df = pd.DataFrame({'GpID': [1,1,0,1,1,0,1,1,2,2,1,1,2,1,1,0,1,2,0,1,1],
'HR': [1,1,1,1,1,1,1, 2,2,2,2,1,1,1, 2,2,2,2,3,3,3],
'Date_': [1,1,1,2,2,2,2, 2,2,2,2,3,3,3, 3,3,3,3,3,3,3]
})
输出格式与类似
df_out = pd.DataFrame({ 'HR': [1,2,3,1,2,3],
'Date_': [1,1,1,2,2,2],
'GpID_0': [1,2,5,1,4,2],
'GpID_1': [1,2,5,1,4,2],
'GpID_2': [4,2,5,1,4,2],
})
尝试:
# 1st try
df_g = df.groupby(["Hr", "Date_"], observed=False).count().fillna(0).unstack()
# 2nd try
df_g = df.groupby(["Hr", "Date_","GpId"], observed=False).count().fillna(0).unstack(-1)
# 3rd try
df_g = df.groupby(["Hr", "Date_"], observed=False).count().fillna(0).unstack()
还不准确
答案 0 :(得分:1)
我相信您试图做这样的事情
In [1]:
import pandas as pd
df = pd.DataFrame({'GpID': [1,1,0,1,1,0,1,1,2,2,1,1,2,1,1,0,1,2,0,1,1],
'HR': [1,1,1,1,1,1,1, 2,2,2,2,1,1,1, 2,2,2,2,3,3,3],
'Date_': [1,1,1,2,2,2,2, 2,2,2,2,3,3,3, 3,3,3,3,3,3,3]
})
df.loc[:,'Count']=1
pd.pivot_table(df, values='Count', index=['Date_', 'HR'], columns=['GpID'], aggfunc='count').fillna(0).reset_index()
Out [1]:
Date_ HR 0 1 2
0 1 1 1 2 0
1 2 1 1 3 0
2 2 2 0 2 2
3 3 1 0 2 1
4 3 2 1 2 1
5 3 3 1 2 0