2016-01-20 60 views
0

我很可能在理解熊貓分組時遇到了麻煩,並且能夠生成按類別疊加的直方圖。熊貓根據日期分類的直方圖,並按類別排序

下面是我正在嘗試做的一個工作示例。真的,我正在循環遍歷許多文件,每個文件都創建一本字典,然後將其附加到包含所有字典的列表中。然後我把它變成一個數據框並將日期字符串轉換爲日期時間對象。

import pandas as pd 

# Stand in for dictionaries created by looping over some files 
d1={'fruit':'banana','vege':'spinach','date':'August 1, 2014'} 
d2={'fruit':'banana','vege':'carrots','date':'August 1, 2014'} 
d3={'fruit':'banana','vege':'peas','date':'August 1, 2015'} 
d4={'fruit':'orange','vege':'spinach','date':'August 1, 2014'} 
d5={'fruit':'orange','vege':'carrots','date':'August 1, 2015'} 
data=[d1,d2,d3,d4,d5] 

# Create the dataframe, turn the date strings into datetime objects 
df=pd.DataFrame(data) 
df.date2=pd.to_datetime(df.date) 

# This attempt at plotting gets me a histogram by year, but not divided how it should be. 
df.groupby(df.date2.dt.year).count().plot(kind="bar") 

產生的情節是這樣的:

Histogram by year, but unsure why 3 bars for each with category labels

我真正喜歡的是這樣的:

Histogram by year, stacked by the text within the category of "fruit"

我已經試過各種其他的東西,如

fr=df.groupby('fruit') 

但隨後fr.plot失敗,因爲

TypeError: Empty 'DataFrame': no numeric data to plot 

預先感謝您的幫助!

回答

2

如何:

df.groupby(df.date2.dt.year)['fruit']\ 
    .value_counts()\ 
    .unstack(1)\ 
    .plot(kind='bar', stacked=True) 

其中產量: enter image description here

0

我會建議使用dateDateTimeIndex。對於pandas 0.17

df['date'] = pd.to_datetime(df.date).dt.year 
df.set_index('date', inplace=True) 
df.groupby(level='date').fruit.value_counts().unstack('fruit').plot.bar(stacked=True) 

enter image description here

+0

不確定的,如果它是大熊貓或Python版本的差異,但你的最後一行並沒有爲我工作。但是,如果我這樣做,它會工作:df.groupby(level ='date')。fruit.value_counts()。unstack()。plot(kind ='bar',stacked = True)。如果我將「水果」包含在疊加偏差中,我會遇到一個錯誤,如果我使用plot.bar,也會出現錯誤。使用熊貓版本0.16.1。 – user5817303

+0

需要熊貓= 0.17 – Stefan