Solve – df.append() is not appending to the DataFrame in Python

Solve – df.append() is not appending to the DataFrame in Python is an issue which happens in Python when you do not Assign df.append back.

In this article I am going to explain why we have the df.append() error and how to solve it using multiple solutions which may work for your particular case.

Explaining the issue : df.append() is not appending to the DataFrame in Python

The error happens when you forget to Assign df.append back

data.append(…) does not work on its own, you need to append it. but before we start solving the issue we need to reproduce the issue.

Let us take the following example

                                                                       #
columnsList=['O','M','N','P']
df8=pd.DataFrame(columns=columnsList)
L=['value oo','value mm','value nn','value pp']
s = pd.Series(dict(zip(df8.columns, L)))
df8.append(s,ignore_index=True)
df8.append(s,ignore_index=True)
                                                                       #

We are supposed to get a dataframe 2X4 with added values but that is not the case.

Instead, this is what we get

                                                                       #
print(df8.shape)
#>>> (0,4)
                                                                       #

Bellow are some solutions that may solve your issue.

Solution : assign the python variable back

Unlike most python errors there is only one way to solve this issue The solution is to assign the result back.

Let us start with a general and simple explanation. The line of code bellow will not work.

                                                                       #
data.append(sub_data) 
                                                                       #

Replacing it with the code bellow will fix the issue

                                                                       #
data=data.append(sub_data)
                                                                       #

For the main example, this is how the output will look like, there is no error, the error is gone.

                                                                       #
          O         M         N         P
0  value oo  value mm  value nn  value pp
1  value oo  value mm  value nn  value pp
                                                                       #

The error above was hard to deal with, I spent hours looking for a proper solution or a set of solutions, I hope this was helpful.

Summing-up

This is the end of our article, this is a confusing error but with a small investigation the error could be solved, thank you for reading.

To support us consider donating to our Kofi account above. Keep coding, keep learning and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/