执行drop方法报错:'function'对象无'drop'属性,如何删除指定列?
Hey there! Let's break down what's going on here and fix this issue step by step.
The Root Cause
That error message spells out the problem clearly: your Amy1 variable isn't a pandas DataFrame—it's a function instead. The drop() method is exclusive to pandas DataFrames, so when you try to call it on a function object, Python throws that AttributeError.
How to Fix It
Follow these steps to get your DataFrame sorted and your column deletion working:
Verify the type of Amy1
First, run this line to confirm whatAmy1actually is:print(type(Amy1))If the output is
<class 'function'>, that confirms our suspicion—you're working with a function, not the DataFrame you thought you had.Correct how you're loading your data
Most likely, you mixed up a function definition or forgot to execute a data-loading function. Here are the two common scenarios:- You defined a function named
Amy1but never called it:
Fix this by calling the function:def Amy1(): return pd.read_csv('your_data_file.csv') # Oops—you didn't call the function to get the DataFrame!Amy1 = Amy1() - You assigned the data-loading function itself to
Amy1instead of its result:# Wrong—this stores the function, not the loaded data Amy1 = pd.read_csv # Correct—call the function with your file path to get the DataFrame Amy1 = pd.read_csv('your_data_file.csv')
- You defined a function named
Confirm you have a valid DataFrame
Run the type check again:print(type(Amy1))You should see
<class 'pandas.core.frame.DataFrame'>—that means you're ready to proceed.Run your original column deletion code
Now thatAmy1is a proper DataFrame, your original code will work as expected:Amy2 = Amy1.drop(columns=['측정소명', '측정소코드']) print(Amy2.shape) Amy2.head()
Quick Extra Check
If you're still stuck, double-check that your data-loading code doesn't have hidden issues—like a wrong file path, missing dependencies, or a function that doesn't actually return a DataFrame.
内容的提问来源于stack exchange,提问作者김현재

