Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

I have several dataframes (of the same shape) that I want to append creating one larger data-frame. Tee individual data-frames all have the type:

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

and look like:

C-Mastersheet.xlsx

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

D-Mastersheet.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-Mastersheet.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

I am trying to return the output:

  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

I am trying to do this using the follwing code whioch loops around a directory:

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)

I am however getting the following type error:

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

I have not come across this before.

like image 440
Stacey Avatar asked Jan 09 '19 14:01

Stacey


1 Answers

This is the problem:

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

The reason behind the error is the first argument of a method is the class instance. In this case, the class instance is inferred to be DeptReturn and there is no 'other' argument supplied.

What you need is:

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

For the first argument we have the class instance df, since the method is being applied on that instance. The second argument is DeptReturn.

See also: What is the purpose of self?

like image 66
jpp Avatar answered Nov 02 '22 21:11

jpp