简体   繁体   中英

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

我有一个空的数据帧 [] 并且想要使用 for 循环附加额外的数据帧而不覆盖现有的数据帧,常规附加方法是覆盖现有的数据帧并仅在输出中显示最后附加的数据帧。

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])

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))

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)

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

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

pandas.concat documentation

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM