Append DF with different lengths Python

39 views Asked by At

I have two (or more) loops and inside each one I create a DF. I want to show the following results in a new DF. I tried to code the following:

table = pd.DataFrame(columns=['col_x', 'col_y', 'col_3', 'predict'])

for x in ['a', 'b', 'c']:
    for y in ['d', 'e', 'f']:
        filtered_df = df[(df[col_1] == x) & (df[col_2] == y)]
        s1 = filtered_df[col_3]
        s2 is a serie (predictions after linear regression with this filtered_df)
        
        table['col_x'] = [x]*len(filtered_df)
        table['col_y'] = [y]*len(filtered_df)
        table['col_3'] = s1
        table['predict'] = s2

So in the first loop, I want to have series 's1' and 's2' in the two last columns of 'table'. And the first two columns with the same data ('a' and 'd' in the first loop).

In the following loops I want to complete my table. Each loop can have different lengths.

I don't know how to append dataframes (or make unions like in SQL). Also, the way I assign my columns didn't work.

Thank you in advanced.

1

There are 1 answers

0
Lukas Hestermeyer On

In order to "union" two dataframes, try pd.concat. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html

E.g.

df_union = pd.concat([df1,df2])