SP1
SP1

Reputation: 31

how to append a dataframe without overwriting existing dataframe using for loop in python

i have an empty dataframe[] and want to append additional dataframes using for loop without overwriting existing dataframes, regular append method is overwriting the existing dataframe and showing only the last appended dataframe in output.

Upvotes: 2

Views: 10540

Answers (5)

Oliver Prislan
Oliver Prislan

Reputation: 330

The question is already well answered, my 5cts are the suggestion to use ignore_index=True option to get a continuous new index, not duplicate the older ones.

import pandas as pd

df_to_append = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB')) # sample
df = pd.DataFrame() # this is a placeholder for the destination

for i in range(3):
    df = df.append(df_to_append, ignore_index=True)

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71610

You can't also use set:

df_new = pd.concat({df_empty, df_additional})

Because pandas.DataFrame objects can't be hashed, set needs hashed so that's why

Or tuple:

df_new = pd.concat((df_empty, df_additional))

They are little quicker...

Update for for loop:

df = pd.DataFrame(data)

for i in range(your number):
    df_new=function_to_get_df_new()
    df = pd.concat({df, df_new}) # or tuple: df = pd.concat((df, df_new))

Upvotes: 1

ipramusinto
ipramusinto

Reputation: 2668

Let you have list of dataframes list_of_df = [df1, df2, df3].

You have empty dataframe df = pd.Dataframe()

If you want to append all dataframes in list into that empty dataframe df:

for i in list_of_df:
    df = df.append(i)

Above loop will not change df1, df2, df3. But df will change. Note that doing df.append(df1) will not change df, unless you assign it back to df so that df = df.append(df1)

Upvotes: 1

Nirali Khoda
Nirali Khoda

Reputation: 388

I don't think you need to use for loop here, try concat()

import pandas
result = pandas.concat([emptydf,additionaldf])

pandas.concat documentation

Upvotes: 0

Cut7er
Cut7er

Reputation: 1237

use concat() from the pandas module.

import pandas as pd
df_new = pd.concat([df_empty, df_additional])

read more about it in the pandas Docs.

regarding the question in the comment...

df = pd.DataFrame(insert columns which your to-be-appended-df has too)

for i in range(10):
    function_to_get_df_new()
    df = pd.concat([df, df_new])

Upvotes: 1

Related Questions