turtle_in_mind
turtle_in_mind

Reputation: 1152

Combining multiple DataFrames and removing identical names

I have a DataFrame df1 that contains two columns, n and MAE Linear. I have another DataFrame df2 that contains n and MAE quadratic.

Now, initially, I had a function called getData(cash, forward, '4C', 'linear') that would do some calculation and return the DataFrame that contains n and the type of interpolation I am doing.

Hence: df1 = getData(cash, forward, '4C', 'linear') and likewise for df2 = getData(..., 'quadratic')

Now, when I try pd.concat([df1, df2], axis=1), I get [n, MAE Linear, n, MAE quadratic].

How do I write it so that I only get [n, MAE Linear, MAE quadratic]?

Upvotes: 1

Views: 49

Answers (2)

Alex Riley
Alex Riley

Reputation: 176978

It sounds like you need to merge the two DataFrames on the column n:

>>> df1.merge(df2, on='n', how='outer')

Here, a new DataFrame with the columns n, MAE Linear, MAE quadratic will be created, with the rows from df1 and df2 aligned on the values in the n column.

Upvotes: 1

ericmjl
ericmjl

Reputation: 14714

Try this:

df1.set_index('n', inplace=True)
df2.set_index('n', inplace=True)

joined = df1.join(df2)

Upvotes: 0

Related Questions