sales and products with exact data given.keys containing the two keys 'store_id' and 'product_id'.merge function with on=keys to join sales and products.sales and products with exact data given.keys containing the two keys 'store_id' and 'product_id'.merge function with on=keys to join sales and products.sales with these rows: {'store_id': 1, 'product_id': 101, 'units_sold': 5}, {'store_id': 1, 'product_id': 102, 'units_sold': 3}, {'store_id': 2, 'product_id': 101, 'units_sold': 2}. Also create a pandas DataFrame called products with these rows: {'store_id': 1, 'product_id': 101, 'product_name': 'Pen', 'category': 'Stationery'}, {'store_id': 1, 'product_id': 102, 'product_name': 'Notebook', 'category': 'Stationery'}, {'store_id': 2, 'product_id': 101, 'product_name': 'Pen', 'category': 'Stationery'}.Use pd.DataFrame with a list of dictionaries to create each DataFrame.
keys that contains the strings 'store_id' and 'product_id' in that order.Use a Python list with the two key strings in order.
merged by merging sales and products using pd.merge with the on=keys argument.Use pd.merge(sales, products, on=keys) to join on multiple columns.
merged DataFrame.Use print(merged) to show the combined table.
What does merging on multiple keys in pandas allow you to do?
Choose the best explanation.
Which of the following is the correct syntax to merge two pandas DataFrames df1 and df2 on columns 'city' and 'year'?
import pandas as pd # Assume df1 and df2 are defined merged_df =
on with a list of column names.on=['city', 'year'], which is correct. Others use wrong parameters like keys or by.on for columns list in merge [OK]Given the following dataframes:
import pandas as pd
df1 = pd.DataFrame({
'city': ['NY', 'LA', 'NY', 'SF'],
'year': [2020, 2020, 2021, 2020],
'value1': [10, 20, 30, 40]
})
df2 = pd.DataFrame({
'city': ['NY', 'LA', 'NY', 'SF'],
'year': [2020, 2021, 2021, 2020],
'value2': [100, 200, 300, 400]
})
merged = pd.merge(df1, df2, on=['city', 'year'], how='inner')
print(merged)What will be the output?
Consider this code snippet:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [1, 2], 'B': [3, 5]})
merged = pd.merge(df1, df2, on=['A', 'B'])
print(merged)What is the issue if the output is an empty dataframe?
You have two dataframes:
df_sales = pd.DataFrame({
'store': ['A', 'B', 'A', 'B'],
'month': [1, 1, 2, 2],
'sales': [100, 200, 150, 250]
})
df_targets = pd.DataFrame({
'store': ['A', 'B'],
'month': [1, 2],
'target': [120, 230]
})You want to merge these to compare sales with targets for matching store and month. Which merge type should you use to keep all sales data and add targets where available?
how='left' to keep all sales rows and add matching targets. -> Option BPass the test and this pattern moves unknown