Search code examples
pythonpandasdataframemergeconcatenation

How do I CONCAT data from a dataframe to another dataframe?


I have built the following function and now .append will be removed from pandas in a future version. So I am weeling to convert this code with concat.

def MyDF(self,DF1,DF2):
    OutputDf = pd.DataFrame([]).reset_index(drop=True)
    for i in range(0,len(DF2)):
        OutputDf = OutputDf.append(DF2.loc[[i]])
        OutputDf = OutputDf.append(DF1.loc[(DF1['TheName'] == DF2['TheName'][i]) & (DF1['WGT'].apply(lambda x: float(x)) > 0) ])
        OutputDf = OutputDf.reset_index(drop=True)
    return OutputDf

I don't know how to use concat in this case, so how would I avoid .append there ?

Not sure that would work :

OutputDf = pd.Concat(OutputDf,DF2.loc[[i]])

Solution

  • pandas.DataFrame.append and pandas.Series.append are Deprecated since version 1.4.0. See Deprecated DataFrame.append and Series.append

    The alternative is using pandas.concat.

    In OP's case, .append() is being used in two cases:

    1. OutputDf = OutputDf.append(DF2.loc[[i]])

    2. OutputDf = OutputDf.append(DF1.loc[(DF1['TheName'] == DF2['TheName'][i]) & (DF1['WGT'].apply(lambda x: float(x)) > 0) ])


    Case 1

    One can change to the following

    OutputDf = pd.concat([OutputDf, DF2.loc[[i]]], ignore_index=True)
    

    Case 2

    One can change to the following

    OutputDf = pd.concat([OutputDf, DF1.loc[(DF1['TheName'] == DF2['TheName'][i]) & (DF1['WGT'].apply(lambda x: float(x)) > 0) ]], ignore_index=True)
    

    Notes:

    • As I do not have access to the dataframes and do not know the desired output, one might have to do some adjustments.