pandas AttributeError: 'numpy.ndarray' 对象没有属性 'drop'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51293196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'numpy.ndarray' object has no attribute 'drop'
提问by RockAndaHardPlace
I'm trying to delete the first 24 rows of my pandas dataframe.
我正在尝试删除 Pandas 数据框的前 24 行。
Searching on the web has led me to believe that the best way to do this is by using the pandas 'drop' function.
在网上搜索让我相信最好的方法是使用 pandas 的“drop”功能。
However, whenever I try to use it, I get the error:
但是,每当我尝试使用它时,都会收到错误消息:
AttributeError: 'numpy.ndarray' object has no attribute 'drop'
This is how I created my pandas dataframe:
这就是我创建Pandas数据框的方式:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
%matplotlib inline
import os
cwd = os.getcwd()
df = pd.read_csv('C:/Users/.../Datasets/Weather/temperature4.csv')
Then:
然后:
df.fillna(df.mean())
df.dropna()
The head of my dataframe looks like this:
And then:
进而:
df = StandardScaler().fit_transform(df)
df.drop(df.index[0, 23], inplace=True)
This is where I get the attributeerror.
这是我得到属性错误的地方。
Not sure what I should do to delete the first 24 rows of my dataframe.
不知道我应该怎么做才能删除数据框的前 24 行。
(This was all done using Python 3 on a Jupyter notebook on my local machine)
(这一切都是在我本地机器上的 Jupyter 笔记本上使用 Python 3 完成的)
回答by tobsecret
The problem lies in the following line:
问题在于以下行:
df = StandardScaler().fit_transform(df)
It returns a numpy array (see docs), which does not have a drop function.
You would have to convert it into a pd.DataFrame
first!
它返回一个 numpy 数组(参见文档),它没有 drop 函数。您必须将其转换为pd.DataFrame
第一个!
new_df = pd.DataFrame(StandardScaler().fit_transform(df), columns=df.columns, index=df.index)