代码之家  ›  专栏  ›  技术社区  ›  Stacey

TypeError:append()缺少1个必需的位置参数:“other”

  •  0
  • Stacey  · 技术社区  · 5 年前

    我有几个数据帧(形状相同),我想附加它们来创建一个更大的数据帧。每个数据帧都具有以下类型:

    C-Mastersheet.xlsx   <class 'pandas.core.frame.DataFrame'>
    D-Mastersheet.xlsx   <class 'pandas.core.frame.DataFrame'>
    L-Mastersheet.xlsx   <class 'pandas.core.frame.DataFrame'>
    

    看起来像:

    C-母版。xlsx

       First Name  Last name        Dept  Location  Status      Concat 
    0          Jo      Jones    Accounts   Bristol Current     JonesJo
    1         Sid      Smith       Sales      Hull     New    SmithSid
    

    D-母版。xlsx

           First Name  Last name        Dept  Location  Status      Concat 
    0            Phil      Evans  Production      Hull Current   EvansPhil
    1           Sarah      Heath   Marketing   Bristol Current  HeathSarah
    2            Jane       Hill    Accounts   Bristol Current    HillJane
    3             Amy     Cooper       Sales      Hull Current   CooperAmy
    

    L-母版。xlsx

       First Name  Last name        Dept  Location  Status      Concat
    0      Marcus      Price  Operations      Hull Current PriceMarcus
    1      Andrew       King      Design   Bristol Current  KingAndrew
    2        Emma       Lane   Marketing   Bristol Current    LaneEmma
    3       Brian       Deen    Accounts   Bristol Current   DeenBrian       
    4       Steve      Jacks      Design   Bristol Current  JacksSteve
    

    我正在尝试返回输出:

      First Name  Last name        Dept  Location   Status      Concat 
     0         Jo      Jones    Accounts   Bristol Current     JonesJo
     1        Sid      Smith       Sales      Hull New        SmithSid
     2       Phil      Evans  Production      Hull Current   EvansPhil
     3      Sarah      Heath   Marketing   Bristol Current  HeathSarah
     4       Jane       Hill    Accounts   Bristol Current    HillJane
     5        Amy     Cooper       Sales      Hull Current   CooperAmy
     6     Marcus      Price  Operations      Hull Current PriceMarcus
     7     Andrew       King      Design   Bristol Current  KingAndrew
     8       Emma       Lane   Marketing   Bristol Current    LaneEmma
     9      Brian       Deen    Accounts   Bristol Current   DeenBrian       
    10      Steve      Jacks      Design   Bristol Current  JacksSteve
    

    我试图使用以下代码来实现这一点,whioch在目录中循环:

    ConsolidatedData = pd.DataFrame
    
    for i in os.listdir(os.chdir(returnsfolder)):
        if i.endswith(".xlsx"):
            )
            rawFilePath = returnsfolder +'\\'+ i
    
            DeptReturn = openRawDeptReturn(rawFilePath)
    
            ConsolidatedData = ConsolidatedData.append(DeptReturn,ignore_index=True)
    

    TypeError: append() missing 1 required positional argument: 'other'
    

    我以前从未遇到过这种情况。

    1 回复  |  直到 4 年前
        1
  •  30
  •   jpp    5 年前

    这就是问题所在:

    df = pd.DataFrame           # returns class
    df = df.append(DeptReturn)  # TypeError: append() missing 1 required positional argument: 'other'
    

    错误背后的原因是 第一 方法的参数是类实例。在本例中,类实例被推断为 DeptReturn 而且没有 'other' 提供了参数。

    你需要的是:

    df = pd.DataFrame()         # returns class instance
    df = df.append(DeptReturn)  # returns instance with method applied
    

    对于第一个论点,我们有全班同学 例子 df ,因为该方法正在该实例上应用。第二个论点是 DeptReturn .

    另见: What is the purpose of self?