Recall & Review
beginner
What does the
melt() function do in pandas?The
melt() function transforms a DataFrame from wide format to long format by unpivoting selected columns into rows.Click to reveal answer
beginner
In
pandas.melt(), what is the role of the id_vars parameter?id_vars specifies the columns to keep as identifiers (stay fixed) while other columns are unpivoted into rows.Click to reveal answer
intermediate
How do
var_name and value_name parameters affect the output of melt()?var_name sets the name of the new column that holds the original column names. value_name sets the name of the new column that holds the values from those columns.Click to reveal answer
beginner
Why would you use
melt() in data analysis?You use
melt() to reshape data for easier analysis, especially when you want to convert wide tables into a tidy long format suitable for plotting or grouping.Click to reveal answer
intermediate
Given a DataFrame with columns
['Name', 'Math', 'Science'], how would melt() transform it?It would turn the subject columns ('Math', 'Science') into rows under one column (e.g., 'Subject'), with their scores in another column (e.g., 'Score'), keeping 'Name' as the identifier.
Click to reveal answer
What is the default behavior of
pandas.melt() if id_vars is not specified?✗ Incorrect
If
id_vars is not set, all columns are unpivoted into two columns: variable and value.Which parameter in
melt() controls the name of the column that holds the original column names?✗ Incorrect
var_name sets the name of the new column that contains the original column names.If you want to keep columns 'A' and 'B' fixed and unpivot the rest, how do you specify
melt()?✗ Incorrect
Use
id_vars=['A', 'B'] to keep those columns fixed during unpivoting.What type of data format does
melt() help create?✗ Incorrect
melt() converts data from wide to long format.Which of these is NOT a typical use case for
melt()?✗ Incorrect
Combining DataFrames vertically is done with functions like
concat(), not melt().Explain how the
melt() function works and why it is useful in data analysis.Think about turning columns into rows to make data easier to analyze.
You got /6 concepts.
Describe a real-life example where you would use
melt() to reshape your data.Imagine you have test scores for students in multiple subjects.
You got /4 concepts.