2017-04-20 64 views
1

我創建了這個數據:KeyError異常簡單的數據集regplot - AttributeError的: '海峽' 對象有沒有屬性 '結合'

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 
sns.set() 

data = np.array([['', 'Gross ', 'Target'], 
       [0, 728, 500], 
       [1, 701, 750], 
       [2, 590, 570], 
       [3, 548, 596]]) 

df = pd.DataFrame(data=data[1:, 1:], columns=data[0, 1:]) 
df 

    Gross Target 
0 728  500 
1 701  750 
2 590  570 
3 548  596 

,並試圖繪製:

sns.regplot(x='Gross', y='Target', data=df) 

結果:KeyError: 'Gross'

--- UPDATE ---

固定的空白,我後接收:

AttributeError: 'str' object has no attribute 'conjugate'

回答

1

有字符串,whitespace將其轉化爲柱:

print (df.columns.tolist()) 
['Gross ', 'Target'] 

一種可能的解決方案是strip

df.columns = df.columns.str.strip() 
print (df.columns.tolist()) 
['Gross', 'Target'] 

或添加空格到列名:

sns.regplot(x='Gross ', y='Target', data=df) 

print (df.dtypes) 
Gross  object 
Target object 
dtype: object 

sns.regplot(x='Gross', y='Target', data=df.astype(int)) 

graph

+0

Hmm.I沒有注意到,thanks.But,現在它給了我:'AttributeError的: '海峽' 對象有沒有屬性「conjugate'' – George

+0

看來你有老版本的熊貓。你的版本是什麼? – jezrael

+0

我正在使用0.19.2 – George

相關問題