'AttributeError: 'function' object has no attribute 'drop'

When I run

Amy2 = Amy1.drop(columns = ['측정소명', '측정소코드'])
print(Amy2.shape)
Amy2.head()

I get the error from the title:

AttributeError: 'function' object has no attribute 'drop'

I managed to load the information of Amy1, but Python can't drop the topic of the columns. I want to eliminate some columns.



Solution 1:[1]

We encounter this error when trying to access an object’s unavailable attribute. Check if Amy1 is defined as a dataframe type of object(I guessed that is your goal).

It is also recommended that you get a copy of the one that you make operations:

Amy2 = Amy1.copy()

Then you can directly drop:

Amy2.drop(columns = ['????', '?????'])

Solution 2:[2]

You are getting this error because you are not calling the dataframe object. To resolve this, you need to use the .copy() method on the DataFrame to get a copy of it, then you can now drop the irrelevant columns from the new DataFrame.

amy2 = amy1.copy()   # make a copy of the dataframe object
amy2.drop(columns = ['col1', 'col2'], inplace=True)   # drop the irrelevant columns
print(amy2.shape)
amy2.head()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zhrist
Solution 2 Victor E. Irekponor