Reputation: 257
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
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
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
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