SJG
SJG

Reputation: 21

Pandas - Append rows to DataFrames as elements in a list, not working

I have a list of dataframes that looks like this:

mylist=[pd.DataFrame({'a':[5,5],'b':[5,5]}),pd.DataFrame({'a':[6,6],'b':[6,6]})]

and a separate datframe (df) that I want to append to the bottom of the two dataframes in mylist.

df=pd.DataFrame({'a':[9],'b':[9]})

When I append by looping over mylist, the append works on the element of the list, but it does not update or change mylist.

for i in mylist:
    i=i.append(df, ignore_index=True)
    print(i, '\n')
    print(mylist, '\n')

   a  b
0  5  5
1  5  5
2  9  9 

[   a  b
 0  5  5
 1  5  5,
    a  b
 0  6  6
 1  6  6] 

    a  b
 0  6  6
 1  6  6
 2  9  9 

[   a  b
 0  5  5
 1  5  5,
    a  b
 0  6  6
 1  6  6] 

I have tried multiple ways around this by using concat and by merging on 'a' and I get the same general result-- the dataframe elements are modified within the loop, but after the loop finishes nothing has changed.

When I try to make the same changes but without using a loop, then mylist is updated.

mylist[0]=mylist[0].append(df, ignore_index=True)
print(mylist[0])
print(mylist)

   a  b
0  5  5
1  5  5
2  9  9

[   a  b
 0  5  5
 1  5  5
 2  9  9,
    a  b
 0  6  6
 1  6  6]

Can someone please explain why this behavior happens in the for loop? Thank you!

Upvotes: 2

Views: 92

Answers (2)

gehbiszumeis
gehbiszumeis

Reputation: 3711

In addition to append: If you want to use pd.concat your input has to be a sequence of pandasobjects

for i in range(len(mylist)):
    mylist[i] = pd.concat([mylist[i], df])     # Passing the two dataframes as list, note the []

Upvotes: 0

piRSquared
piRSquared

Reputation: 294288

append is not an inplace operation.

Refer to the indices of the list and modify the positions

for i in range(len(mylist)):
    mylist[i] = mylist[i].append(df, ignore_index=True)

Upvotes: 2

Related Questions