Reshaping data means changing the layout or structure of data without changing its values. For example, turning rows into columns or grouping data differently to make it easier to analyze.
Click to reveal answer
beginner
What is the purpose of transposing a DataFrame?
Transposing a DataFrame swaps its rows and columns. This means rows become columns and columns become rows. It helps to view or analyze data from a different angle.
Click to reveal answer
intermediate
How does the pandas function `pivot()` help in reshaping data?
The `pivot()` function rearranges data by turning unique values from one column into new columns, organizing data in a wide format. It’s useful to summarize or compare data easily.
Click to reveal answer
intermediate
Explain the difference between `melt()` and `pivot()` in pandas.
`melt()` converts wide data into long format by turning columns into rows, while `pivot()` does the opposite by turning rows into columns. They are opposite reshaping operations.
Click to reveal answer
beginner
What happens if you transpose a non-square DataFrame?
When you transpose a non-square DataFrame (different number of rows and columns), the shape changes so that the number of rows becomes the number of columns and vice versa. The data values stay the same but their positions change.
Click to reveal answer
Which pandas function would you use to turn columns into rows?
Agroupby()
Bpivot()
Cmelt()
Dtranspose()
✗ Incorrect
`melt()` converts columns into rows, changing data from wide to long format.
What does the transpose operation do to a DataFrame?
ASwaps rows and columns
BFilters rows
CSorts the data
DChanges data values
✗ Incorrect
Transposing swaps rows and columns without changing the data values.
If you want to create new columns from unique values in a column, which function is best?
Aconcat()
Bmelt()
Ctranspose()
Dpivot()
✗ Incorrect
`pivot()` creates new columns from unique values in a column.
What is the shape of a DataFrame after transposing a 3x5 DataFrame?
A5x3
B3x5
C8x1
D1x8
✗ Incorrect
Transposing swaps rows and columns, so 3x5 becomes 5x3.
Which operation would you use to convert long data into wide format?
Amelt()
Bpivot()
Cgroupby()
Dsort_values()
✗ Incorrect
`pivot()` converts long data into wide format by spreading values across columns.
Describe how you would reshape a DataFrame from long to wide format using pandas.
Think about turning rows into columns.
You got /3 concepts.
Explain what happens when you transpose a DataFrame and why it might be useful.
Consider how data orientation changes.
You got /3 concepts.
Practice
(1/5)
1. What does the transpose() function do to a DataFrame in pandas?
easy
A. It swaps the rows and columns of the DataFrame.
B. It sorts the DataFrame by index.
C. It removes duplicate rows from the DataFrame.
D. It fills missing values with zeros.
Solution
Step 1: Understand transpose operation
The transpose() function flips the DataFrame so rows become columns and columns become rows.
Step 2: Compare with other options
Sorting, removing duplicates, or filling missing values are different operations not related to transposing.
Final Answer:
It swaps the rows and columns of the DataFrame. -> Option A
Quick Check:
transpose() = swap rows and columns [OK]
Hint: Remember transpose flips rows and columns [OK]
Common Mistakes:
Confusing transpose with sorting
Thinking transpose removes duplicates
Assuming transpose fills missing values
2. Which of the following is the correct syntax to reshape a DataFrame df from wide to long format using pandas?
easy
A. df.melt(id_vars=['id'], var_name='variable', value_name='value')
B. df.stack(level=0)
C. df.transpose()
D. df.pivot(index='id', columns='variable', values='value')
Solution
Step 1: Identify function for wide to long
The melt() function converts wide format to long format by unpivoting columns.
Step 2: Check syntax correctness
df.melt(id_vars=['id'], var_name='variable', value_name='value') is the correct syntax to keep 'id' fixed and melt other columns.
Final Answer:
df.melt(id_vars=['id'], var_name='variable', value_name='value') -> Option A
Quick Check:
melt() = wide to long reshape [OK]
Hint: Use melt() to go from wide to long format [OK]
C. A DataFrame with rows and columns swapped, showing columns as rows.
D. A DataFrame with duplicated rows removed.
Solution
Step 1: Understand df.T usage
T is a shortcut for transpose() in pandas, swapping rows and columns.
Step 2: Visualize the transposed DataFrame
Original df has columns 'A' and 'B' with 2 rows. After transpose, columns become rows and rows become columns.
Final Answer:
A DataFrame with rows and columns swapped, showing columns as rows. -> Option C
Quick Check:
df.T = transpose DataFrame [OK]
Hint: Use df.T to quickly transpose DataFrame [OK]
Common Mistakes:
Thinking df.T sorts data
Believing df.T removes duplicates
Assuming df.T causes error
4. What is wrong with this code snippet that tries to reshape a DataFrame df using pivot()?
df.pivot(index='id', columns='variable')
medium
A. Pivot cannot be used on DataFrames with numeric columns.
B. Missing the values parameter required for pivot.
C. The index parameter should be a list, not a string.
D. The columns parameter should be omitted.
Solution
Step 1: Check pivot() required parameters
The pivot() function needs index, columns, and values to reshape data properly.
Step 2: Identify missing argument
The code misses the values argument, which tells pivot what data to fill in the new table.
Final Answer:
Missing the values parameter required for pivot. -> Option B
Quick Check:
pivot() needs values parameter [OK]
Hint: Always include values= when using pivot() [OK]
Common Mistakes:
Forgetting values parameter in pivot()
Using string instead of list for index incorrectly
Thinking pivot() can't handle numeric columns
5. You have a DataFrame df with columns ['id', 'year', 'sales_Q1', 'sales_Q2']. You want to reshape it to a long format with columns ['id', 'year', 'quarter', 'sales']. Which code correctly achieves this?
hard
A. df.stack(level=['quarter', 'sales'])
B. df.pivot(index=['id', 'year'], columns='quarter', values='sales')
C. df.transpose()
D. df.melt(id_vars=['id', 'year'], var_name='quarter', value_name='sales')
Solution
Step 1: Identify wide to long reshape method
To convert columns like 'sales_Q1', 'sales_Q2' into rows, use melt().
Step 2: Use melt() with correct parameters
Keep 'id' and 'year' fixed with id_vars, set var_name='quarter' and value_name='sales' to rename columns.
Step 3: Check other options
pivot() is for long to wide, transpose() swaps rows and columns, stack() is for multi-index reshaping but not here.
Final Answer:
df.melt(id_vars=['id', 'year'], var_name='quarter', value_name='sales') -> Option D
Quick Check:
melt() with id_vars = long format reshape [OK]
Hint: Use melt() with id_vars to reshape wide to long [OK]
Common Mistakes:
Using pivot() instead of melt() for wide to long
Trying transpose() for reshaping columns
Misusing stack() without multi-index
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.