0
我有一個多指標大熊貓數據幀像這樣聚集在大熊貓據幀計數
# df =
val
date id
2021-01-01 whatever1 0
whatever2 1
whatever3 0
whatever4 3
whatever5 2
2021-01-02 whatever2 0
whatever7 3
2021-01-03 whatever3 0
whatever4 0
...
,我想算不同值的出現次數的第一指標下,像這樣,
0 1 2 3
2021-01-01 2 1 1 1
2021-01-02 1 0 0 1
2021-01-03 2 0 0 0
...
我該怎麼做?我最好的嘗試是這樣的:
df.groupby(by='date', level=0).agg([lambda x: [np.count_nonzero(x==i) for i in range(df.values.max())]])
# result =
val
<lambda>
data
2021-01-01 [2, 1, 1, 1]
2021-01-02 [1, 0, 0, 1]
2021-01-03 [2, 0, 0, 0]
謝謝你,我一直在學習新的功能。 – Bella