Chelsea Lewis
Chelsea Lewis

Reputation: 257

Unable to drop column, object has no attribute error

I have a csv file with column titles: name, mfr, type, calories, protein, fat, sodium, fiber, carbo, sugars, vitamins, rating. When I try to drop the sodium column, I don't understand why I'm getting a NoneType' object has no attribute 'drop' error

I've tried

df.drop(['sodium'],axis=1) 

df = df.drop(['sodium'],axis=1)

df = df.drop (['sodium'], 1, inplace=True)

Upvotes: 4

Views: 9959

Answers (3)

Satyam Annu
Satyam Annu

Reputation: 165

use pd.read_csv(r'File_Path_with_name') and this will be sorted out as there is some issue with reading csv file.

Upvotes: 0

prodigal_son
prodigal_son

Reputation: 116

There is a similar question, you should have a look at,

Delete column from pandas DataFrame using del df.column_name

According to the answer,

`df = df.drop (['sodium'], 1, inplace=True)`

should rather be

df.drop (['sodium'], 1, inplace=True)

Although the first code,

df = df.drop(['sodium'],axis=1)

should work fine, if there is an error, try

print(df.columns)

to make sure that the columns are actually read from the csv file

Upvotes: 3

kindall
kindall

Reputation: 184071

Here's your problem:

df = df.drop (['sodium'], 1, inplace=True)

This returns None (documentation) due to the inplace flag, and so you no longer have a reference to your dataframe. df is now None and None has no drop attribute.

My expectation is that you have done this (or something like it, perhaps dropping another column?) at some prior point in your code.

Upvotes: 3

Related Questions