Recall & Review
beginner
What is the purpose of converting data from long to wide format?
Converting data from long to wide format reshapes the data so that each unique value in a specified column becomes a new column. This makes it easier to compare values side by side, like turning a list of measurements into a table with separate columns for each measurement type.
Click to reveal answer
beginner
Which pandas function is commonly used to convert data from long to wide format?
The pandas function
pivot() or pivot_table() is used to convert data from long to wide format. They rearrange the data by setting one column as the new columns and another as the values.Click to reveal answer
intermediate
What is the difference between
pivot() and pivot_table() in pandas?pivot() requires unique index/column pairs and will fail if duplicates exist. pivot_table() can handle duplicates by aggregating values using a function like mean or sum.Click to reveal answer
beginner
Given a DataFrame with columns
['Date', 'City', 'Temperature'] in long format, how would you convert it to wide format with cities as columns?Use
df.pivot(index='Date', columns='City', values='Temperature'). This makes each city a column and rows indexed by date, showing temperatures side by side.Click to reveal answer
intermediate
Why might you need to reset the index after pivoting a DataFrame?
Pivoting often sets one or more columns as the index. Resetting the index with
reset_index() turns the index back into regular columns, which can be easier to work with or export.Click to reveal answer
Which pandas function would you use to convert a DataFrame from long to wide format when there are duplicate entries for the same index and column?
✗ Incorrect
pivot_table() can handle duplicates by aggregating values, while pivot() cannot.
In a long format DataFrame, which column typically becomes the new columns in wide format after pivoting?
✗ Incorrect
The column specified in the columns= parameter becomes the new columns in the wide format.
What happens if you try to use
pivot() on data with duplicate index-column pairs?✗ Incorrect
pivot() requires unique index-column pairs and will raise an error if duplicates exist.
After pivoting, why might you want to use
reset_index()?✗ Incorrect
reset_index() converts the index back into regular columns for easier access.
Which of these is NOT a typical use case for converting long to wide format?
✗ Incorrect
Converting long to wide format usually increases the number of columns, not reduces them.
Explain how to convert a DataFrame from long to wide format using pandas. Include the key function and parameters.
Think about which columns become rows, columns, and values.
You got /4 concepts.
Describe the difference between pivot() and pivot_table() in pandas and when to use each.
Consider how duplicates in data affect the choice.
You got /4 concepts.