更新时间:2023-12-01 19:22:52
从数据框开始:
In [9]: df
Out[9]:
date animals quantity
0 2015-02-10 dogs 1
1 2015-02-11 cats 2
2 2015-02-11 pigs 5
您可以使用pivot
方法指定应将哪些列用作索引,列名称和值:
You can use the pivot
method specifying which columns should be used as the index, as the column names, and as the values:
In [10]: df.pivot(index='date', columns='animals', values='quantity').fillna(0)
Out[10]:
animals cats dogs pigs
date
2015-02-10 0 1 0
2015-02-11 2 0 5
除了动物"和数量"列之外,这还可以为您提供所需的输出.他们需要在那里吗?
This gets you the desired output, apart from the 'animals' and 'quantity' columns. Are they needed to be there?