AttributeError: 'numpy.ndarray' object has no attribute 'drop'

RockAndaHardPlace picture RockAndaHardPlace · Jul 11, 2018 · Viewed 26k times · Source

I'm trying to delete the first 24 rows of my pandas dataframe.

Searching on the web has led me to believe that the best way to do this is by using the pandas 'drop' function.

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:

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: enter image description here

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.

(This was all done using Python 3 on a Jupyter notebook on my local machine)

Answer

tobsecret picture tobsecret · Jul 11, 2018

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!

new_df = pd.DataFrame(StandardScaler().fit_transform(df), columns=df.columns, index=df.index)