无法在熊猫中绘制groupby对象

时间:2019-06-29 12:51:10

标签: python pandas matplotlib

我已将groupby方法应用于数据框df,以便获得X列(百分比)的每个值的平均值

df1 = df.groupby('percentage')['ratio'].mean()

对于每个百分比值,我都有一个比率值的平均值(因为我有很多数据点)。

现在我想绘制新的百分比(X)与新的比率值(Y),但我不知何故。 df1 [:,0]给我一条错误消息,好像不是我的第一列的写调用一样。

如何绘制这两列?

这是df1的输出:

percentage
0.000000    0.987699
0.000144    0.974359
0.000461    0.930000
0.001427    0.880549
0.006119    0.968185
0.008497    0.968686
0.017821    0.970008
0.028747    0.976759
0.030128    0.975607
0.038823    0.979795
0.043440    0.979847
Name: ratio, Length: 61, dtype: float64

4 个答案:

答案 0 :(得分:2)

df1pandas.Series。对于熊猫,可以将maplotlib与内置函数(如pandas.plot())一起使用。您可以这样做:

import matplotlib.pyplot as plt
axes = df1.plot()
axes.set_ylabel(df1.name)
plt.show()

enter image description here

答案 1 :(得分:1)

嗯,这确实很奇怪,但是df1是一个系列,而不是数据帧。左边的线是序列索引,而右边的是值,因此print(df[0.017821])将打印0.970008,您仍然可以访问这些值

percentage = list(df1.index)
ratio = df1.values

这就是为什么您收到错误消息的原因,系列只有一个轴

答案 2 :(得分:1)

只需绘制<div class="noticias"> <a href="<?php the_permalink(); ?>"> <?the_post_thumbnail();?> </a> <h1 style="margin-top:-30px"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1> <div><p><?php echo wp_trim_words( get_the_content(), 50 ); ?></p></div> </div> </div>

df1

答案 3 :(得分:1)

您无法访问特定的列,因为您要处理序列而不是数据框。

type(df.groupby('percentage')['ratio'].mean())
# pandas.core.series.Series

我认为.reset_index()应该会有所帮助,因为它将结果转换为数据框

type(df.groupby('percentage')['ratio'].mean().reset_index())
# pandas.core.frame.DataFrame

因此,一旦使用.reset_index(),就可以将列分配给变量:

new_df = df.groupby('percentage')['ratio'].mean().reset_index()
x, y = (new_df['percentage'], new_df['ratio'])