What is the output DataFrame after performing an inner join on the 'key' column?
import pandas as pd df1 = pd.DataFrame({'key': ['A', 'B', 'C'], 'val1': [1, 2, 3]}) df2 = pd.DataFrame({'key': ['B', 'C', 'D'], 'val2': [4, 5, 6]}) result = pd.merge(df1, df2, on='key', how='inner') print(result)
Inner join keeps only keys present in both DataFrames.
Inner join returns rows where the key exists in both DataFrames. Here, keys 'B' and 'C' are common.
How many rows will the resulting DataFrame have after performing a left join on 'key'?
import pandas as pd df1 = pd.DataFrame({'key': ['X', 'Y', 'Z'], 'val1': [10, 20, 30]}) df2 = pd.DataFrame({'key': ['Y', 'Z'], 'val2': [100, 200]}) result = pd.merge(df1, df2, on='key', how='left') print(len(result))
Left join keeps all rows from the left DataFrame.
Left join keeps all rows from df1 (3 rows), matching rows from df2 or NaN if no match.
What error will this code raise?
import pandas as pd df1 = pd.DataFrame({'id': [1, 2], 'val': ['A', 'B']}) df2 = pd.DataFrame({'key': [1, 2], 'val2': ['X', 'Y']}) result = pd.merge(df1, df2, on='id')
Check if both DataFrames have the column used in 'on'.
df2 does not have a column named 'id', so merge raises a KeyError.
Which option shows the correct DataFrame after an outer join on 'key'?
import pandas as pd df1 = pd.DataFrame({'key': ['A', 'B'], 'val1': [1, 2]}) df2 = pd.DataFrame({'key': ['B', 'C'], 'val2': [3, 4]}) result = pd.merge(df1, df2, on='key', how='outer').sort_values('key').reset_index(drop=True) print(result)
Outer join keeps all keys from both DataFrames, filling missing values with NaN.
Outer join includes keys A, B, and C. Values missing in one DataFrame become NaN.
You have two DataFrames with overlapping column names besides the keys. Which code correctly merges them on 'id' and 'date' with suffixes to avoid column name conflicts?
import pandas as pd df1 = pd.DataFrame({'id': [1, 2], 'date': ['2023-01-01', '2023-01-02'], 'value': [10, 20]}) df2 = pd.DataFrame({'id': [1, 2], 'date': ['2023-01-01', '2023-01-02'], 'value': [100, 200]})
Suffixes must be a tuple of strings.
The suffixes parameter requires a tuple of two strings to append to overlapping column names.