FAQ: Working with Multiple DataFrames - Inner Merge I

This community-built FAQ covers the “Inner Merge I” exercise from the lesson “Working with Multiple DataFrames”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Analysis with Pandas

FAQs on the exercise Inner Merge I

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I don’t understand the instruction on this exercise that says to " Give your answer as a string assigned to the variable order_3_description ."

I know that a string would be inside quotes, but I don’t know what the format is for a command that assigns it to a variable. I tried:
order_3_description = “_____”

But that was not it.

Well, I think that basically was correct, but I needed a space before and after the = sign

Instead of typing the string directly, I tried to get the answer by using methods in Pandas:

order_3_description = products[products.product_id == orders[orders.order_id == 3].product_id.values[0]].description.values[0]

order_5_phone_number = customers[customers.customer_id == orders[orders.order_id == 5].customer_id.values[0]].phone_number.values[0]

Is there a better way to search and retrieve values of a DataFrame?

1 Like

Finally, I just have finished all the exercises in this Lesson. After looking back, I have found that the purpose of this exercise is to motivate us to the following steps (I overlooked the description saying so). A better way will be found by merging multiple DataFrames (which you will learn in the following exercises). My improved answer is:

all_data = orders.merge(products).merge(customers)

order_3_description = all_data[all_data.order_id == 3].description.values[0]

order_5_phone_number = all_data[all_data.order_id == 5].phone_number.values[0]
1 Like

Sorry, I am new in this. Why is it ”…values[0]” In both the cases?

1 Like

I used .values to retrieve the string value ​​from a Pandas Series (such as all_data[all_data.order_id == 3].description) since it is required to give a string, not Series. For example all_data[all_data.order_id == 3] returns the row of the index 4. If we want to get the value from here, it seems that we can use .at or .loc, but in this case, we need to get index 4 beforehand:

# some code needed to know the index (which is 4)
order_3_description = all_data[all_data.order_id == 3].at[4, 'description']

or apply .reset_index():

order_3_description = all_data[all_data.order_id == 3].reset_index().at[0, 'description']

Then I thought if there is a simpler way to get the value. The .values returns an array of values of the Pandas Series. I thought it would be simpler to get the value out of the array.

However, upon further search later, it seems that .values is not a recommended way in the current version of Pandas. To make a Series an array, it is recommended to use .array in this case:

order_3_description = all_data[all_data.order_id == 3].description.array[0]

I tried a bit more, and with .iat (or .iloc) we didn’t have to get index 4 beforehand:

order_3_description = all_data[all_data.order_id == 3].iat[0, 5]

So I do the work to read up on DataFrame merge function. Write the code solution for the instruction questions

merge1 = orders.merge(products, left_on = ‘product_id’, right_on = ‘product_id’)
order_3_description = merge1[‘description’][merge1[‘order_id’] == 3]
print(order_3_description)

merge2 = orders.merge(customers, left_on = ‘customer_id’, right_on = ‘customer_id’)
order_5_phone_number = merge2[‘phone_number’][merge2[‘order_id’] == 5]
print(order_5_phone_number)

Works from the command line but not on Codecademy. ??? Check the solution. Its a hard coded answer by observation that matches my code solution. I would be pisst but I learned something