熊猫:如何根据行值而不是列值来设置boxplot?

时间:2017-03-24 02:38:43

标签: python pandas 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

现在我想为每个玩家做一个boxplot(),所以我们可以看到他们如何相互排名。像这样:

enter image description here

我尝试的第一件事是做一个traspose矩阵的盒子图:

df.T.boxplot()

但我收到错误IndexError: list index out of range

我认为它与traspose中创建的索引有关,所以我一直在玩它们,但我真的不知道还能做什么。

2 个答案:

答案 0 :(得分:3)

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

enter image description here

答案 1 :(得分: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