1

I created following dataframe priceearning_byyear, which is still incomplete:

enter image description here

Afterwards, I would like to "insert"/merge following series price into the above df:

enter image description here

The serie has one row (2021) too much, which I would like to remove when inserting into the above df.

My attempt is:

priceearning_byyear.merge(price.to_frame(), left_index=True)

But I got following error:

MergeError: Must pass right_on or right_index=True

If I use right_index=True, I'll get :

MergeError: Must pass left_on or left_index=True

I don't get it, why it doesn't work. Thank you for any pointer:-)

1

3 Answers 3

3

You are almost there. Just pass left_index and right_index simultaneously to get the desired effect as below.

# Reproduce your data
import pandas as pd
priceearning_byyear = pd.DataFrame(dict(year=[2016,2017,2018,2019,2020], eps=[2.09,2.32,3.00,2.99,3.31])).set_index('year')
price = pd.Series([28.95,42.31,39.44,73.41,132.69,119.99], index=[2016,2017,2018,2019,2020,2021])
price.name = 'Close'
price.index.name='year'

# Merge priceearning_byyear and price by using their indexes
priceearning_byyear.merge(price,left_index=True,right_index=True)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You need to specify the column which you want to merge on in priceearning_byyear.

data1 = {"year":[x for x in range(2016, 2020)], "eps":[x for x in range(2,12,2)]}
data2 = pd.Series(data = {2016: 1, 2017: 2, 2018: 3, 2019: 4, 2020: 5, 2021: 6})

df1 = pd.DataFrame(data1)
df1.merge(data2.to_frame(name='price'), how='left', left_on="year", right_index=True)

result

Comments

0

Another example I run into recently. The index of my dataframe was datetime, and my series was integers. What I did was write a small function to merge the dataframe and series for me in the following steps:

  • first, by converting to the series to a dataframe, and setting its index using the dataframe's index, and
  • second, merging the two dataframes and renaming the series' column.

Note: The function assumes that the dataframe and series have the same length.

def merge_df_series(x, y):
    """
    Merge dataframe and series on index
    and rename series column.
    """
    series = y.to_frame().set_index(x.index)
    return x.merge(series, left_index=True, right_index=True).rename(columns={0: 'y'})

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.