熊猫系列操纵 - 改变x和y

时间:2016-11-10 19:31:28

标签: python pandas

我有一个pandas.core.series.Series对象x,当我print x时会输出以下内容:

2     681
1     575
3     573
4     381
0     340

如何将x更改为x2,以便print x2输出:

681     2
575     1
573     3
381     4
340     0

我想这样做,否则当我尝试绘制直方图(x.plot.hist())时,我不会得到我想要的结果。

2 个答案:

答案 0 :(得分:2)

您可以使用Series构造函数,如有必要,可以添加参数name

print (pd.Series(x.index, index=x.values))
681    2
575    1
573    3
381    4
340    0
dtype: int64

print (pd.Series(x.index, index=x.values, name='a'))
681    2
575    1
573    3
381    4
340    0
Name: a, dtype: int64

但似乎你需要Series.plot.bar

x.plot.bar()

答案 1 :(得分:2)

你想要条形图吗?

s.plot.bar()

enter image description here

pd.Series(s.index, index=s.values, name='a').plot.bar()

enter image description here

直方图没有任何意义,因为它是以图形方式表示聚合数据的一种方式。你的系列只有5个长度......没有真正的聚合。

相关问题