0

I'm trying to get data associated with certain literary texts from one dataframe into another. In the first dataframe, there are two columns, one with title info, and one with the section of the novel. For example

Title Section
Book1 beginning
Book1 middle
Book1 end
Book2 beginning
Book2 middle
Book2 end

In a second dataframe, I have categorical data about each book. For example:

Title American
Book1 yes
Book2 no

I need to merge them on Title so that the 'American' values from the second dataframe duplicate in the first dataframe, as follows:

Title Section American
Book1 beginning yes
Book1 middle yes
Book1 end yes
Book2 beginning no
Book2 middle no
Book2 end no

The closest I've gotten (based on the [pandas merge guide]Pandas Merging 101 is:

df4.merge(df5["American"], left_on=["Title"], right_on=["Title"], how = "left")

Without have both left_on and right_on, I get an error demanding a right_on value, despite the documentation. However, now I am getting a KeyError: 'Title', even though Title is definitely a column header in both dataframes?

claudiae
  • 35
  • 4

1 Answers1

1

You are getting key error just because you are trying to merge df4 with just "American" column of df5. Therefore it doesn't see the "Title" column on df5.

Try this one :

df4.merge(df5, how = "left",on="Title")
Ugur Yigit
  • 155
  • 7