2017-02-25 75 views
0

我有這樣一個數據幀:如何在數據框中分配索引的常用元素?

  type count 
batsman   
V Kohli  4 361 
V Kohli  6 149 
SK Raina 5 1 
SK Raina 6 161 
RG Sharma 5 1 
RG Sharma 6 164 

現在我們可以看到,有索引commmon條目。我想繪製BARH情節,所以我想俱樂部的索引到一個使數據幀看起來是這樣的:

  type count 
batsman   
V Kohli  4 361 
      6 149 
SK Raina 5 1 
      6 161 
RG Sharma 5 1 
      6 164 

我該怎麼做呢?

+0

你通過_clubbing_指標究竟意味着什麼?什麼是空白行的索引值? – miradulo

+0

沒有空白行 – user517696

+0

索引中的空白行,我的意思是。 – miradulo

回答

0

您可以將索引分配到新列並執行groupby。

df['batsman'] = df.index 
grouped_df = df.groupby(['batsman', 'type']).agg({'count': 'sum'}) 
# grouped_df 

      type count 
batsman   
V Kohli  4 361 
      6 149 
SK Raina 5 1 
      6 161 
RG Sharma 5 1 
      6 164 
0

您可以groupby您的數據幀的指數和「類型」欄,然後彙總得到您的BARH情節合理編組輸出。

df.groupby([df.index,'type'])['count'].sum().plot.barh(title='Counts') 

enter image description here

相關問題