0

For example, I have two Dataframes like these:

data
0 100
1 150
2 200
3 250
4 300

and

data2
1 1
2 2
3 3

I'd like to make a new Dataframe like this:

data data2
1 150 1
2 200 2
3 250 3

How can I achieve this?

wohylee04
  • 1
  • 1

1 Answers1

0

You can use concat and dropna:

new_df = pd.concat([df1, df2], axis=1).dropna().astype({**df1.dtypes, **df2.dtypes})

Output:

>>> new_df
   data  data2
1   150      1
2   200      2
3   250      3

Or, even shorter:

new_df = df1.merge(df2, how='inner', right_index=True, left_index=True)