2016-02-14 41 views
-1

我想從由couchdb形成的pandas dataframe列繪製數據。這是什麼樣的代碼和輸出數據:Seaborn地塊kdeplot但不是distplot

print df4.Patient_Age 

Doc_ID 
000103f8-7f48-4afd-b532-8e6c1028d965 99 
00021ec5-9945-47f7-bfda-59cf8918f10b 92 
0002510f-fb89-11e3-a6eb-742f68319ca7 32 
00025550-9a97-44a4-84d9-1f6f7741f973 73 
0002d1b8-b576-4db7-af55-b3f26f7ca63d 49 
0002d40f-2b45-11e3-8f66-742f68319ca7 42 
000307eb-18a6-47cd-bb03-33e484fad029 18 
00033d3d-1345-4739-9522-b41b8db3ee23 42 
00036d2e-0a51-4cfb-93d1-3e137a026f19 42 
0003b054-5f3b-4553-8104-f71d7a940d84 10 
Name: Patient_Age, dtype: object 

如果我執行此代碼:

sns.kdeplot(df4.Patient_Age) 

預期該地塊產生。然而,當我運行此:

sns.distplot(df4.Patient_Age) 

我碰到下面的錯誤與distplot:

TypeError: unsupported operand type(s) for /: 'unicode' and 'long' 

要糾正這個錯誤,我用:

df4.Patient_Age = [int(i) for i in df4.Patient_Age] 
all(isinstance(item,int) for item in df4.Patient_Age) 

輸出是:

False 

我想要的是什麼t和是:

  1. 爲什麼被先前生成的kdeplot但不是histplot?
  2. 當我將數據類型更改爲int時,爲什麼仍然得到False?如果數據不是int(如False所示),那麼histplot爲什麼在轉換後有效?
+3

請給我們一個[MCVE。 – cel

回答

0

問題是您的值不是數字。如果你強制他們整數或浮動,它會起作用。

from io import StringIO 

import pandas 
import seaborn 
seaborn.set(style='ticks') 

data = StringIO("""\ 
Doc_ID         Age 
000103f8-7f48-4afd-b532-8e6c1028d965 99 
00021ec5-9945-47f7-bfda-59cf8918f10b 92 
0002510f-fb89-11e3-a6eb-742f68319ca7 32 
00025550-9a97-44a4-84d9-1f6f7741f973 73 
0002d1b8-b576-4db7-af55-b3f26f7ca63d 49 
0002d40f-2b45-11e3-8f66-742f68319ca7 42 
000307eb-18a6-47cd-bb03-33e484fad029 18 
00033d3d-1345-4739-9522-b41b8db3ee23 42 
00036d2e-0a51-4cfb-93d1-3e137a026f19 42 
0003b054-5f3b-4553-8104-f71d7a940d84 10 
""") 

df = pandas.read_table(data, sep='\s+') 
df['Age'] = df['Age'].astype(float) 

df.info() 
# prints 

<class 'pandas.core.frame.DataFrame'> 
Int64Index: 10 entries, 0 to 9 
Data columns (total 2 columns): 
Doc_ID 10 non-null object 
Age  10 non-null float64 
dtypes: float64(1), object(1) 
memory usage: 240.0+ bytes 

那麼接下來:

seaborn.distplot(df['Age'])

給我:

enter image description here