0

I have two separate dataframes, df1 and df2. I want to be able to create a new dataframe based on a row from df1 and row from df2.

I need to be able to create a new df, based on row index [1] of df1 & row index [2] of df2

Below is df1

df1

   A      B      C
0  1.57   4.40   6.00
1  1.73   3.95   5.00
2  1.26   6.60   12.00 

df2


df2

   D      E       F
0  1.57   4.48    6.25
1  1.27   6.73    11.88
2  1.74   3.99    5.11


Expected New_Df

   A      B      C     D      E       F
0  1.73   3.95   5.00  1.74   3.99    5.11
1
  • The row are many, each df has dynamic rows numbers, df1 has 28, df2 has 35. Commented Apr 15, 2022 at 17:15

1 Answer 1

1

You can try pd.concat

out = pd.concat([df1.iloc[1], df2.iloc[2]], axis=0).to_frame().T
print(out)

      A     B    C     D     E     F
0  1.73  3.95  5.0  1.74  3.99  5.11
Sign up to request clarification or add additional context in comments.

9 Comments

Does this solution apply, if i need to pick the rows randomly from each dataframe?
@Bernietechy Yes, just replace the 1 and 2 with your randomly picked row index.
some rows, give me this key error: 0, not quite sure why but other rows are able to concat.
@Bernietechy Do the two dataframe index starts from 0?
yes, the dataframes starts at index 0
|

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.