答案 0 :(得分:0)
value_counts
+ melt
您可以将value_counts
应用于每个系列,然后融化数据框:
df = pd.DataFrame({'01-01-2018': [1, 2, 3, 4, 1, 2],
'02-01-2018': [4, 2, 3, 1, 3, 1]})
res = df.apply(pd.Series.value_counts).reset_index()
res = pd.melt(res, id_vars='index')
res.columns = ['Code', 'Date', 'Freq']
print(res)
Code Date Freq
0 1 01-01-2018 2
1 2 01-01-2018 2
2 3 01-01-2018 1
3 4 01-01-2018 1
4 1 02-01-2018 2
5 2 02-01-2018 1
6 3 02-01-2018 2
7 4 02-01-2018 1