我有一個pandas.core.series.Series
對象x
輸出以下,當我做print x
:熊貓系列操作 - 改變x和y
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()
),我沒有得到我想要的結果。
我有一個pandas.core.series.Series
對象x
輸出以下,當我做print x
:熊貓系列操作 - 改變x和y
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()
),我沒有得到我想要的結果。
您可以使用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()
查看當我用我的數據繪製直方圖時得到的[結果](https://i.gyazo.com/f13816ce0fa45070a35c3243e567fb59.png)。右邊是我使用你的答案時得到的。你的回答是正確的,但這不是我想要的直方圖。我想讓x = 0向我展示y = 340,x = 1以顯示y = 575等。爲什麼會這樣? – dimitris93
嗯,可能需要'x.plot.bar()',因爲'hist'計數值 - 得到值的頻率 – jezrael
除非你向我們展示,否則我們應該如何知道你想要什麼? @ dimitris93 – piRSquared