让我们说我在表单上有一个熊猫DataFrame:
data = [['January', 'January', 'January', 'February', 'February', 'February',
'March', 'March', 'March'],
['Snow', 'Sun', 'Snow', 'Overcast', 'Overcast', 'Sun', 'Rain', 'Rain',
'Sun']]
df = pd.DataFrame(index=data[0], data=data[1])
看起来像这样:
0
January Snow
January Sun
January Snow
February Overcast
February Overcast
February Sun
March Rain
March Rain
March Sun
现在,我想创建一个以月为索引,天气为列名并计数为数据的DataFrame。也就是说,我想要一个看起来像这样的DataFrame:
Snow Sun Overcast Rain
January 2 1 0 0
February 0 1 2 0
March 0 1 0 2
这应该很简单,但是我找不到解决方法。我可以使用
df.groupby(['Month', 'Weather']).size()
但是我不能用它来创建我想要的DataFrame。希望你能帮助我!