我有一个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()
)时,我不会得到我想要的结果。
答案 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)