[英]AttributeError: 'numpy.ndarray' object has no attribute 'drop'
[英]AttributeError - 'numpy.ndarray' object has no attribute 'drop'
對於當前的項目,我計划在包含數值數據的 CSV 集上運行 scikit-learn Stochastic Graduent Booster 算法。
當調用腳本的X = Germany.drop('Status', axis='columns')
時,我收到一個AttributeError: 'numpy.ndarray' object has no attribute 'drop'
。
我認為這個錯誤可能與我正在轉換 CSV 數據pd.to_numeric
的事實有關,這可能也轉換了字符串標題。 是否有任何智能調整可以使此運行?
CSV 數據具有以下結構:
相應的代碼如下所示:
Germany = pd.read_csv('./Germany_filtered.csv', index_col=0)
Germany = Germany.fillna("")
Germany = pd.to_numeric(Germany.columns.str, errors='coerce')
Germany.head()
X = Germany.drop('Status', axis='columns')
y = Germany['Status']
In [167]: df = pd.DataFrame(np.arange(12).reshape(3,4),columns=['a','b','c','d'])
drop
在數據框上工作正常:
In [168]: df.drop('c',axis='columns')
Out[168]:
a b d
0 0 1 3
1 4 5 7
2 8 9 11
to_numeric
生成一個 numpy 數組:
In [169]: x = pd.to_numeric(df.columns.str,errors='coerce')
In [170]: x
Out[170]:
array(<pandas.core.strings.StringMethods object at 0x7fef602862b0>,
dtype=object)
In [171]: type(x)
Out[171]: numpy.ndarray
它應該抱怨head
,讓之前drop
:
In [172]: x.head()
Traceback (most recent call last):
File "<ipython-input-172-830ed5e65d76>", line 1, in <module>
x.head()
AttributeError: 'numpy.ndarray' object has no attribute 'head'
In [173]: x.drop()
Traceback (most recent call last):
File "<ipython-input-173-6d3a33341569>", line 1, in <module>
x.drop()
AttributeError: 'numpy.ndarray' object has no attribute 'drop'
to_numeric
文檔怎么說? 我沒有使用過,但顯然您不想將df.columns.str
對象傳遞給它。 我沒有使用過這個函數,但讓我們嘗試將數據幀傳遞給它:
In [176]: x = pd.to_numeric(df,errors='coerce')
Traceback (most recent call last):
File "<ipython-input-176-d095b0166b8f>", line 1, in <module>
x = pd.to_numeric(df,errors='coerce')
File "/usr/local/lib/python3.6/dist-packages/pandas/core/tools/numeric.py", line 139, in to_numeric
raise TypeError("arg must be a list, tuple, 1-d array, or Series")
TypeError: arg must be a list, tuple, 1-d array, or Series
所以讓我們傳遞一個列/系列:
In [177]: x = pd.to_numeric(df['a'],errors='coerce')
In [178]: x
Out[178]:
0 0
1 4
2 8
Name: a, dtype: int64
結果Series
可以分配回數據幀,在同一列或新列中:
In [179]: df['a'] = x
In [180]: df
Out[180]:
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
現在,在我的示例框架中,無需進行此轉換,但它應該可以為您提供一些有用的東西。
讓我們嘗試一個真正的字符串轉換:
In [195]: df['a'] = ['00','04','LS']
In [196]: df
Out[196]:
a b c d
0 00 1 2 3
1 04 5 6 7
2 LS 9 10 11
鏈接的答案沒有幫助:
In [197]: pd.to_numeric(df.columns.str, errors='coerce')
Out[197]:
array(<pandas.core.strings.StringMethods object at 0x7fef602862b0>,
dtype=object)
但我的版本確實產生了一個數字系列:
In [198]: pd.to_numeric(df['a'], errors='coerce')
Out[198]:
0 0.0
1 4.0
2 NaN
Name: a, dtype: float64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.