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)

RockAndaHardPlace
  • 417
  • 1
  • 7
  • 18
  • Try `df = df.iloc[24:]` – Arda Arslan Jul 11 '18 at 19:47
  • @ArdaArslan When I tried that, I got the AttributeError: 'numpy.ndarray' object has no attribute 'iloc' – RockAndaHardPlace Jul 11 '18 at 19:49
  • 1
    [fit_transaform()](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html#sklearn.preprocessing.StandardScaler.fit_transform) returns a numpy array. You are treating it as a dataframe. Try `df = pd.DataFrame(StandardScaler().fit_transform(df))` . – harvpan Jul 11 '18 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. – lhay86 Jul 11 '18 at 19:54
  • @Ihay86 Very helpful insights, I'll look into them, thanks much! – RockAndaHardPlace Jul 11 '18 at 20:11

1 Answers1

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)
tobsecret
  • 2,442
  • 15
  • 26