2017-03-24 16 views
3

我有一個遊戲,我和朋友一起玩一些得分數據,它看起來像:熊貓:如何做一個boxplot基於行值而不是列值?

df = pd.DataFrame({'Player' : ['A', 'B', 'C', 'D', 'E'], 
        'Score1' : [100, 150, 110, 180, 125], 
        'Score2' : [200, 210, np.nan, 125, 293], 
        'Score3' : [50, 35, 200, 100, 180]}) 

如果我做了df.boxplot()我會根據得分#箱線圖,即,基於在整個社會的分數:

enter image description here

現在我想要做的每個球員的箱線圖(),所以我們可以看到他們是如何排名反目成仇。事情是這樣的:

enter image description here

我想的第一件事是應該做的traspose矩陣的箱線圖:

df.T.boxplot() 

但我得到一個錯誤IndexError: list index out of range

我認爲它必須做的隨着traspose中創建的索引,所以我一直在玩他們,但我真的不知道還有什麼要做。

回答

3

您需要設置索引球員

import pandas as pd 
import numpy as np 

df = pd.DataFrame({'Player' : ['A', 'B', 'C', 'D', 'E'], 
        'Score1' : [100, 150, 110, 180, 125], 
        'Score2' : [200, 210, np.nan, 125, 293], 
        'Score3' : [50, 35, 200, 100, 180]}) 
df = df.set_index('Player') 
print df 
df.T.boxplot() 

enter image description here

+0

謝謝你們,你真棒。我花了至少半個小時的時間,這非常簡單。 – luisfer

3
tdf = df.set_index('Player').T 
tdf.boxplot() 
plt.show() 

enter image description here

相關問題