2016-11-10 28 views
2

我有一個pandas.core.series.Series對象x輸出以下,當我做print x熊貓系列操作 - 改變x和y

2  681 
1  575 
3  573 
4  381 
0  340 

我怎樣才能改變xx2使print x2輸出:

681  2 
575  1 
573  3 
381  4 
340  0 

我想這樣做,否則當我嘗試繪製直方圖(x.plot.hist()),我沒有得到我想要的結果。

回答

2

你想條形圖?

s.plot.bar() 

enter image description here

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

enter image description here


直方圖是沒有意義的作爲其一種方法,以圖形方式表示的聚集數據。你的系列只是長度5 ...沒有什麼可以真正聚合的。

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() 
+0

查看當我用我的數據繪製直方圖時得到的[結果](https://i.gyazo.com/f13816ce0fa45070a35c3243e567fb59.png)。右邊是我使用你的答案時得到的。你的回答是正確的,但這不是我想要的直方圖。我想讓x = 0向我展示y = 340,x = 1以顯示y = 575等。爲什麼會這樣? – dimitris93

+0

嗯,可能需要'x.plot.bar()',因爲'hist'計數值 - 得到值的頻率 – jezrael

+0

除非你向我們展示,否則我們應該如何知道你想要什麼? @ dimitris93 – piRSquared