Answers:
如果[0, 1, 2]是数字标签而不是索引,则pandas.DataFrame.pivot_table可以:
在[]中:
数据= pd.DataFrame.from_records(
[[0,'A'],[0,'B'],[1,'B'],[1,'C'],[1,'D'],[2,'B'],[ 2,'D']],
column = ['number_label','category'])
data.pivot_table(index = ['number_label'],列= ['category'],aggfunc = [len],fill_value = 0)
出[]:
伦
类别ABCD
number_label
0 1 1 0 0
1 0 1 1 1
2 0 1 0 1
这篇博客文章很有帮助:http : //pbpython.com/pandas-pivot-table-explained.html
如果[0, 1, 2]是索引,则collections.Counter很有用:
在[]中:
data2 = pd.DataFrame.from_dict(
{'categories':{0:['A','B'],1:['B','C','D'],2:['B','D']}}))
data3 = data2 ['categories']。apply(collections.Counter)
pd.DataFrame.from_records(data3).fillna(value = 0)
出[]:
A B C D
0 1 1 0 0
1 0 1 1 1
2 0 1 0 1