1

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!

3
  • 1
    df_1 has 7 columns(0-6) and df_2 has 6 columns. So unequal length of columns are there in both dataframes. Commented May 21, 2020 at 11:26
  • rename the columns of df_1 to match df_2? Also, drop the last column of df_1. Then pandas will no which columns should match with which. Commented May 21, 2020 at 11:28
  • Hi Mayank! it was a typo when I copied the example, sorry Commented May 21, 2020 at 11:35

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.