Challenge - 5 Problems
Reshaping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember reshape changes the shape but keeps all elements in order.
✗ Incorrect
The array arr has 6 elements from 0 to 5. Reshaping it to (2,3) creates 2 rows and 3 columns, filling rows first.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Transpose swaps rows and columns.
✗ Incorrect
Original DataFrame has columns A and B with rows 0 and 1. Transpose swaps rows and columns, so columns become rows and vice versa.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check if the total number of elements matches the new shape.
✗ Incorrect
The DataFrame has 6 elements (3 rows * 2 columns). Reshaping to (2,4) requires 8 elements, which is not possible.
❓ visualization
advanced2: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)
Attempts:
2 left
💡 Hint
Pivot uses the index and columns parameters to rearrange data.
✗ Incorrect
Pivot sets 'Date' as index, 'City' as columns, and fills values with 'Temperature'. So each date row shows temperatures for each city column.
🚀 Application
expert3: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)
Attempts:
2 left
💡 Hint
Melt converts columns to rows, then groupby calculates mean per variable.
✗ Incorrect
Melt creates rows for Math and Science scores per ID. Grouping by 'variable' and averaging 'value' gives mean scores for Math and Science.