4

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)
Chelsea Lewis
  • 227
  • 3
  • 6

3 Answers3

3

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.

kindall
  • 178,883
  • 35
  • 278
  • 309
3

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

prodigal_son
  • 116
  • 8
0

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

Satyam Annu
  • 155
  • 1
  • 5