Anurag Pandey
Anurag Pandey

Reputation: 463

how to merge two dataframes if the index and length both do not match?

i have two data frames predictor_df and solution_df like this :

predictor_df 

1000 A B C 1001 1 2 3 1002 4 5 6 1003 7 8 9 1004 Nan Nan Nan

and a solution_df

0 D 1 10 2 11 3 12

the reason for the names is that the predictor_df is used to do some analysis on it's columns to arrive at analysis_df . My analysis leaves the rows with Nan values in predictor_df and hence the shorter solution_df

Now i want to know how to join these two dataframes to obtain my final dataframe as

  A      B    C    D
  1      2    3   10
  4      5    6   11
  7      8    9   12
 Nan    Nan  Nan

please guide me through it . thanks in advance. Edit : i tried to merge the two dataframes but the result comes like this ,

      A      B    C    D
      1      2    3   Nan
      4      5    6   Nan
      7      8    9   Nan
     Nan    Nan  Nan

Edit 2 : also when i do pd.concat([predictor_df, solution_df], axis = 1) it becomes like this

          A         B      C   D
          Nan      Nan   Nan  10
          Nan      Nan   Nan  11
          Nan      Nan   Nan  12
          Nan      Nan   Nan  Nan

Upvotes: 0

Views: 2180

Answers (1)

Nickil Maveli
Nickil Maveli

Reputation: 29711

You could use reset_index with drop=True which resets the index to the default integer index.

pd.concat([df_1.reset_index(drop=True), df_2.reset_index(drop=True)], axis=1)

     A    B    C     D
0    1    2    3  10.0
1    4    5    6  11.0
2    7    8    9  12.0
3  Nan  Nan  Nan   NaN

Upvotes: 2

Related Questions