You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行drop方法报错:'function'对象无'drop'属性,如何删除指定列?

Fixing AttributeError: 'function' object has no attribute '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:

  1. Verify the type of Amy1
    First, run this line to confirm what Amy1 actually 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.

  2. 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 Amy1 but never called it:
      def Amy1():
          return pd.read_csv('your_data_file.csv')
      # Oops—you didn't call the function to get the DataFrame!
      
      Fix this by calling the function:
      Amy1 = Amy1()
      
    • You assigned the data-loading function itself to Amy1 instead 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')
      
  3. 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.

  4. Run your original column deletion code
    Now that Amy1 is 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,提问作者김현재

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 12:24:05