2017-05-04 156 views
2

我創建了一個數據幀,看起來像下面這樣:熊貓數據幀和matplotlib.pyplot

Data frame #1

我沒有問題,下面繪製數據:

df_catch.plot(x='YY', y='ALB_C', kind='scatter', 
     figsize=(12,6), title='ALB catch/hooks') 
plt.xlabel('Year') 
plt.ylabel('ALB catch/hooks') 
plt.show() 

有許多個月和幾年的許多行數據。我想將數據連接到幾年(即每年的月份數據總和)。我這樣做有以下幾點:

name = df_catch.groupby('YY') 
# Apply the sum function to the groupby object 
df_year = name.sum() 
df_year.head(5) 

這產生主要是除了YY數據預期的結果,現在的指數,我嘗試做的就是類似的散點圖任何事情引發錯誤。

Summed data

問題1.有一種優雅的方式做他們一年數據的款項沒有得到YY的數據作爲新的索引。另外請注意,我這樣做的方式是獲得我想避免的所有數據列的總和,例如經度和緯度。

問題2.如果你確實有一個數據變量作爲索引,你如何做一個類似於上面第一個代碼片段的散點圖。我能夠使用下面的代碼得到一個線條圖,但它確實不是我想要的。

plt.plot(df_year.index, df_year['ALB_C']) 

非常感謝您的幫助。我對python/pandas非常陌生,但是就像什麼功能一樣,我通過搜索問題來找到答案,並且在線查看了教程。再次感謝。

回答

1

對於轉換index列2個解決方案:

需要reset_index

name = df_catch.groupby('YY') 
# Apply the sum function to the groupby object 
df_year = name.sum().reset_index() 
df_year.head(5) 

或者添加參數as_index=Falsegroupby

name = df_catch.groupby('YY', as_index=False) 
# Apply the sum function to the groupby object 
df_year = name.sum() 
df_year.head(5) 
1

問題1:讓我們嘗試

name = df_catch.groupby('YY', as_index=False) 

name.sum().reset_index() 

問題2:讓我們做這個

plt.plot(df_year.index, df_year['ALB_C'], marker="o", linestyle='none') 
+1

抱歉,我已經指出了意見錯字! – IanS

+0

@IanS沒問題。感謝您的更正。 –