pandas 熊猫:生成并绘制平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13890673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:32:24  来源:igfitidea点击:

pandas: generate and plot average

pythonmatplotlibplotpandas

提问by vkontori

I have a pandas dataframe like:

我有一个Pandas数据框,如:

In [61]: df = DataFrame(np.random.rand(3,4), index=['art','mcf','mesa'],
                        columns=['pol1','pol2','pol3','pol4'])

In [62]: df
Out[62]: 
          pol1      pol2      pol3      pol4
art   0.661592  0.479202  0.700451  0.345085
mcf   0.235517  0.665981  0.778774  0.610344
mesa  0.838396  0.035648  0.424047  0.866920

and I want to generate a row with the average for the policies across benchmarks and then plot it.

我想用跨基准的策略的平均值生成一行,然后绘制它。

Currently, the way I do this is:

目前,我这样做的方式是:

df = df.T
df['average'] = df.apply(average, axis=1)
df = df.T
df.plot(kind='bar')

Is there an elegant way to avoid the double transposition?

有没有一种优雅的方法来避免双重换位?

I tried:

我试过:

df.append(DataFrame(df.apply(average)).T)
df.plot(kind='bar')

This will append the correct values but does not update the index properly and the graph is messed up.

这将附加正确的值,但不会正确更新索引,并且图表会混乱。

A clarification. The result of the code with the double transposition is this: enter image description hereThis is what I want. To show both the benchmarks and the average of the policies, not just the average. I was just curious if I can do it better.

澄清。双转位的代码结果是这样的:在此处输入图片说明这就是我想要的。显示基准和策略的平均值,而不仅仅是平均值。我只是好奇我是否可以做得更好。

Note that the legend is usually messed up. For a fix:

请注意,图例通常是乱七八糟的。修复:

ax = df.plot(kind='bar')
ax.legend(patches, list(df.columns), loc='best')

回答by bmu

You can simply use the instance method meanof the DataFrameand than plot the results. There is no need for transposition.

您可以简单地使用 的实例方法meanDataFrame然后绘制结果。不需要转位。

In [14]: df.mean()
Out[14]: 
pol1    0.578502
pol2    0.393610
pol3    0.634424
pol4    0.607450

In [15]: df.mean().plot(kind='bar')
Out[15]: <matplotlib.axes.AxesSubplot at 0x4a327d0>

policies.png

政策.png

Update

更新

If you want to plot the bars of all columns and the mean you can appendthe mean:

如果您想绘制所有列的条形图和平均值,您可以append使用平均值:

In [95]: average = df.mean()

In [96]: average.name = 'average'

In [97]: df = df.append(average)

In [98]: df
Out[98]: 
             pol1      pol2      pol3      pol4
art      0.661592  0.479202  0.700451  0.345085
mcf      0.235517  0.665981  0.778774  0.610344
mesa     0.838396  0.035648  0.424047  0.866920
average  0.578502  0.393610  0.634424  0.607450

In [99]: df.plot(kind='bar')
Out[99]: <matplotlib.axes.AxesSubplot at 0x52f4390>

second plot

第二个情节

If your layout doesn't fit in to the subplot tight_layoutwill adjust the matplotlib parameters.

如果您的布局不适合子图,tight_layout则将调整 matplotlib 参数。