This code creates a table with student names and their scores in two subjects. Then it uses melt() to turn the subject columns into one column, making the data longer and easier to analyze.
B. DataFrame with columns: ID, Month, Sales and 4 rows unpivoted from Jan and Feb
C. DataFrame with columns: ID, Jan, Feb unchanged
D. Error due to missing parameters
Solution
Step 1: Understand melt() parameters
id_vars=['ID'] keeps 'ID' fixed. var_name='Month' renames the variable column. value_name='Sales' renames the value column.
Step 2: Predict output structure
Jan and Feb columns become rows under 'Month' with their values under 'Sales'. Result has 4 rows (2 IDs x 2 months).
Final Answer:
DataFrame with columns: ID, Month, Sales and 4 rows unpivoted from Jan and Feb -> Option B
Quick Check:
melt() output = long format with renamed columns [OK]
Hint: melt() creates rows for each value_vars entry per id_vars row [OK]
Common Mistakes:
Expecting original wide format output
Ignoring var_name and value_name renaming
Thinking melt() drops id_vars
4. Consider this code snippet:
df = pd.DataFrame({'ID':[1,2],'A':[10,20],'B':[30,40]})
result = pd.melt(df, id_vars=['A'], value_vars=['ID', 'B'])
What is the main issue with this code?
medium
A. The id_vars column 'A' should be a fixed identifier, but it is a value column here.
B. Syntax error due to wrong parameter names.
C. No issue; code runs correctly and melts as expected.
D. value_vars contains 'ID' which is usually an identifier and should be in id_vars.
Solution
Step 1: Analyze id_vars and value_vars roles
id_vars should be columns to keep fixed (usually identifiers). value_vars are columns to unpivot.
Step 2: Check column roles in code
'ID' is usually an identifier and should be in id_vars, but here it is in value_vars, which is unusual and likely a mistake.
Final Answer:
value_vars contains 'ID' which is usually an identifier and should be in id_vars. -> Option D
Quick Check:
Identifiers belong in id_vars, not value_vars [OK]
Hint: Keep identifiers in id_vars, not in value_vars [OK]
Common Mistakes:
Swapping id_vars and value_vars
Assuming melt() ignores column roles
Thinking code has syntax error
5. You have a DataFrame with columns: ['Date', 'Store', 'Product_A', 'Product_B']. You want to reshape it so each row shows Date, Store, Product (either 'Product_A' or 'Product_B'), and Sales (the value). Which melt() call achieves this?
hard
A. pd.melt(df, id_vars=['Date', 'Store'], var_name='Product', value_name='Sales')
B. pd.melt(df, id_vars=['Product_A', 'Product_B'], var_name='Product', value_name='Sales')
C. pd.melt(df, value_vars=['Date', 'Store'], var_name='Product', value_name='Sales')
D. pd.melt(df, id_vars=['Date'], value_vars=['Store', 'Product_A', 'Product_B'])
Solution
Step 1: Identify fixed and unpivot columns
'Date' and 'Store' are identifiers to keep fixed (id_vars). 'Product_A' and 'Product_B' are value columns to unpivot.
Step 2: Use melt() with correct parameters
Use id_vars=['Date', 'Store'], var_name='Product' to name the unpivoted column, and value_name='Sales' for values.
Final Answer:
pd.melt(df, id_vars=['Date', 'Store'], var_name='Product', value_name='Sales') -> Option A