11

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)

5
  • Try df = df.iloc[24:] Commented Jul 11, 2018 at 19:47
  • @ArdaArslan When I tried that, I got the AttributeError: 'numpy.ndarray' object has no attribute 'iloc' Commented Jul 11, 2018 at 19:49
  • 1
    fit_transaform() returns a numpy array. You are treating it as a dataframe. Try df = pd.DataFrame(StandardScaler().fit_transform(df)) . Commented Jul 11, 2018 at 19:51
  • 1
    A few comments: your image shows that you should probably be passing in column names when you read the csv - it looks like your column names are taken from the first row of data. Try passing in the name parameter in pd.read_csv. Also df.mean() returns a series, so I don't think df.fillna(df.mean()) does what you expect. Also it's a bit dangerous to do df.dropna() after doing df.fillna() - you shouldn't be expecting to lose rows. Lastly, without inplace=True, you aren't actually changing df with any of those commands. Commented Jul 11, 2018 at 19:54
  • @Ihay86 Very helpful insights, I'll look into them, thanks much! Commented Jul 11, 2018 at 20:11

1 Answer 1

18

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.