0
0
Data Analysis Pythondata~20 mins

Reshaping and transposing in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reshaping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of reshaping a NumPy array
What is the output of this code snippet that reshapes a NumPy array?
Data Analysis Python
import numpy as np
arr = np.arange(6)
reshaped = arr.reshape(2, 3)
print(reshaped)
A
[[0 1 2 3]
 [4 5 6 7]]
B
[[0 1]
 [2 3]
 [4 5]]
C
[[0 1 2]
 [3 4 5]]
D[0 1 2 3 4 5]
Attempts:
2 left
💡 Hint
Remember reshape changes the shape but keeps all elements in order.
data_output
intermediate
2:00remaining
Result of pandas DataFrame transpose
Given this DataFrame, what is the result of transposing it?
Data Analysis Python
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
result = df.T
print(result)
A
   0  1
A  1  2
B  3  4
B
   A  B
0  1  3
1  2  4
C
   1  2
A  0  1
B  3  4
D
   0  1
A  3  4
B  1  2
Attempts:
2 left
💡 Hint
Transpose swaps rows and columns.
🔧 Debug
advanced
2:00remaining
Identify the error in reshaping a pandas DataFrame
What error does this code raise and why? import pandas as pd df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}) df_reshaped = df.values.reshape(2, 4)
Data Analysis Python
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]})
df_reshaped = df.values.reshape(2, 4)
ANo error, reshaping works fine
BTypeError: 'DataFrame' object is not callable
CAttributeError: 'DataFrame' object has no attribute 'values'
DValueError: cannot reshape array of size 6 into shape (2,4)
Attempts:
2 left
💡 Hint
Check if the total number of elements matches the new shape.
visualization
advanced
2:30remaining
Visualize the effect of pivoting a DataFrame
What does the pivot operation produce from this DataFrame? import pandas as pd df = pd.DataFrame({ 'Date': ['2023-01-01', '2023-01-01', '2023-01-02', '2023-01-02'], 'City': ['NY', 'LA', 'NY', 'LA'], 'Temperature': [30, 60, 28, 65] }) pivoted = df.pivot(index='Date', columns='City', values='Temperature') print(pivoted)
Data Analysis Python
import pandas as pd
df = pd.DataFrame({
  'Date': ['2023-01-01', '2023-01-01', '2023-01-02', '2023-01-02'],
  'City': ['NY', 'LA', 'NY', 'LA'],
  'Temperature': [30, 60, 28, 65]
})
pivoted = df.pivot(index='Date', columns='City', values='Temperature')
print(pivoted)
A
Date        LA  NY
City               
2023-01-01  30  60
2023-01-02  28  65
B
City        LA  NY
Date               
2023-01-01  60  30
2023-01-02  65  28
C
City        NY  LA
Date               
2023-01-01  30  60
2023-01-02  28  65
D
Date        NY  LA
City               
2023-01-01  60  30
2023-01-02  65  28
Attempts:
2 left
💡 Hint
Pivot uses the index and columns parameters to rearrange data.
🚀 Application
expert
3:00remaining
Transform and summarize data with melt and groupby
Given this DataFrame, which option correctly melts it and then groups by 'variable' to find the mean value? import pandas as pd df = pd.DataFrame({ 'ID': [1, 2], 'Math': [90, 80], 'Science': [85, 95] }) melted = pd.melt(df, id_vars=['ID'], value_vars=['Math', 'Science']) result = melted.groupby('variable')['value'].mean() print(result)
Data Analysis Python
import pandas as pd
df = pd.DataFrame({
  'ID': [1, 2],
  'Math': [90, 80],
  'Science': [85, 95]
})
melted = pd.melt(df, id_vars=['ID'], value_vars=['Math', 'Science'])
result = melted.groupby('variable')['value'].mean()
print(result)
A
Math       85.0
Science    90.0
Name: value, dtype: float64
B
ID
1    87.5
2    87.5
Name: value, dtype: float64
C
Math       90
Science    95
Name: value, dtype: int64
D
variable
Math       85
Science    90
Name: value, dtype: int64
Attempts:
2 left
💡 Hint
Melt converts columns to rows, then groupby calculates mean per variable.