如何绘制pandas数据帧的某些行?

dur*_*hit 5 python matplotlib pandas seaborn

我有这个示例数据帧:

      animal gender     name  first  second  third
0     dog      m      Ben      5       6      3
1     dog      f    Lilly      2       3      5
2     dog      m      Bob      3       2      1
3     cat      f     Puss      1       4      4
4     cat      m  Inboots      3       6      5
5    wolf      f     Lady    NaN       0      3
6    wolf      m   Summer      2       2      1
7    wolf      m     Grey      4       2      3
8    wolf      m     Wind      2       3      5
9    lion      f     Elsa      5       1      4
10   lion      m    Simba      3       3      3
11   lion      f     Nala      4       4      2
Run Code Online (Sandbox Code Playgroud)

现在,我怀疑我可能需要一些分层索引,但我还没有在Pandas那么远.但是,我真的需要做一些(显然太高级)的东西,并没有想出如何做到这一点.基本上,我最终想要的是一个情节(可能是一个散点图,虽然一条线现在也可以正常使用).

1)我想有一个4个子图的数字 - 每个动物的一个子图.每个子情节的标题应该是动物.

2)在每个子图中,我想绘制数字(例如每年出生的小熊数),即给定行的"第一","第二"和"第三"的值,并给它一个标签,这将显示图例中的"名称".对于每个子图(每只动物),我想分别绘制男性和女性(例如蓝色的男性和红色的女性),此外,还绘制动物的平均值(即每个列的平均值)给予动物)黑色.

3)注意事项:将其绘制为1,2,3表示exaple - 参考列号,例如,对于标题为"dog"的第一个子图,我想绘制类似于plt.plot(np.array([1,2,3]),x,'b', np.array([1,2,3]),y,'r', np.array([1,2,3]), np.mean(x,y,axis=1),'k')x的位置(在第一个案例)5,6,3和这个蓝色情节的图例将显示'Ben',y将是2,3,5并且红色情节的图例将显示'Lilly'并且黑色情节将是3.5,4.5 ,4并且在图例中我将定义它是"均值"(对于每个子图).

我希望我做得足够清楚.我明白,如果没有看到结果,可能很难想象它,但......好吧,如果我知道怎么做,我不会问......

总而言之,我想在不同层次上循环数据框架,将动物放在不同的子图上,并比较男性和女性以及它们在每个子图中的平均值.

我的实际数据帧要大得多,所以在理想情况下,我想要一个稳健但易于理解的解决方案(对于编程初学者).

要了解子图应该是什么样子,这是excel中的产品:

简要概述了情节

jrj*_*rjc 1

我不确定我是否理解你的意思。但我认为你需要将数据帧转换为长格式或整洁格式,因为使用该格式您将更容易进行许多操作,首先是根据分类变量制作绘图。

作为df您的数据框,要将其转换为整洁的格式,只需使用:

df2 = pd.melt(df, id_vars=["animal","gender","name"])
df2
  animal gender     name variable  value
0    dog      m      Ben    first    5.0
1    dog      f    Lilly    first    2.0
2    dog      m      Bob    first    3.0
3    cat      f     Puss    first    1.0
4    cat      m  Inboots    first    3.0
...
31   wolf     m     Grey    third    3.0
32   wolf     m     Wind    third    5.0
33   lion     f     Elsa    third    4.0
34   lion     m    Simba    third    3.0
35   lion     f     Nala    third    2.0
Run Code Online (Sandbox Code Playgroud)

然后(几乎)一切都变得简单,只需使用seaborn,如下所示:

g = sns.factorplot(data=df2, # from your Dataframe
                   col="animal", # Make a subplot in columns for each variable in "animal"
                   col_wrap=2, # Maximum number of columns per row 
                   x="variable", # on x-axis make category on the variable "variable" (created by the melt operation)
                   y="value", # The corresponding y values
                   hue="gender", # color according to the column gender
                   kind="strip", # the kind of plot, the closest to what you want is a stripplot, 
                   legend_out=False, # let the legend inside the first subplot.
                   )
Run Code Online (Sandbox Code Playgroud)

然后就可以提高整体的美感:

g.set_xlabels("year")
g.set_titles(template="{col_name}") # otherwise it's "animal = dog", now it's just "dog"
sns.despine(trim=True) # trim the axis.
Run Code Online (Sandbox Code Playgroud)

条形图Seaborn

要添加平均值,恐怕您必须手动执行此操作,但是,如果您有更多数据,您也可以考虑使用箱线图或小提琴图,顺便说一句,您可以在带状图之上使用它们。

我邀请您查看Seaborn 的文档以进一步改进您的情节。

华泰