Iva
Iva

Reputation: 45

append dataframes avoiding NaN python

I have 2 dataframes (df_1) containing 2 rows and 100 columns and df_2 containing 70 rows and 100 columns. I want to append the 2 additional rows of df_1 to the 70 rows of df_2

df_1
                   0        1        2        3        4        5            
   first_point   458.69   457.71   420.82   482.50   501.89   405.89      
    max_point    3654.07  8134.25  7520.39  6913.17  7564.12  5883.32     

df_2
    Mean1   Mean2   Mean3   Mean4   Mean5   Mean6   
0  458.69  457.71  420.82  482.50  501.89  405.89     
1  437.92  339.23  287.35  462.16  405.46  387.76    
2  443.19  303.66  314.83  461.07  349.54  399.97     
3  416.03  315.33  317.84  456.53  390.97  374.84    
4  406.89  306.29  328.26  457.55  456.30  398.38 

I want to append the 2 additional rows of df_1 to the 70 rows of df_2 into df_3

                0       1       2       3       4       5
first_point   458.69  457.71  420.82  482.50  501.89  405.89      
 max_point    3654.07 8134.25 7520.39 6913.17 7564.12 5883.32    
           0  458.69  457.71  420.82  482.50  501.89  405.89     
           1  437.92  339.23  287.35  462.16  405.46  387.76    
           2  443.19  303.66  314.83  461.07  349.54  399.97     
           3  416.03  315.33  317.84  456.53  390.97  374.84    
           4  406.89  306.29  328.26  457.55  456.30  398.38 

I have used

df_3 =df_1.append(df_2)

                  0        1        2        3        4        5        6  \
first_point   458.69   457.71   420.82   482.50   501.89   405.89   480.77   
max_point    3654.07  8134.25  7520.39  6913.17  7564.12  5883.32  5849.17   
0                NaN      NaN      NaN      NaN      NaN      NaN      NaN   
1                NaN      NaN      NaN      NaN      NaN      NaN      NaN   
2                NaN      NaN      NaN      NaN      NaN      NaN      NaN   
3                NaN      NaN      NaN      NaN      NaN      NaN      NaN   
4                NaN      NaN      NaN      NaN      NaN      NaN      NaN   

I would understand if this happens for an unequal number of columns, but this is not the case. I also tried

df_3 =df_1.append(df_02,ignore_index = True)

which led to the same result...what could I do? I have been looking around but I dont seem to find the right answer. Many thanks!

Upvotes: 1

Views: 92

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34046

You can do this:

## rename column names in both dataframes to match each other
In [1412]: df_1.columns = range(df_1.shape[1])
In [1407]: df_2.columns = range(df_2.shape[1])

## now append
In [1414]: df_3 = df_1.append(df_2)    
In [1415]: df_3 
Out[1415]: 
                   0        1        2        3        4        5
first_point   458.69   457.71   420.82   482.50   501.89   405.89
max_point    3654.07  8134.25  7520.39  6913.17  7564.12  5883.32
0             458.69   457.71   420.82   482.50   501.89   405.89
1             437.92   339.23   287.35   462.16   405.46   387.76
2             443.19   303.66   314.83   461.07   349.54   399.97
3             416.03   315.33   317.84   456.53   390.97   374.84
4             406.89   306.29   328.26   457.55   456.30   398.38

Upvotes: 1

Related Questions