Challenge - 5 Problems
Pandas and NumPy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of NumPy array converted to Pandas Series
What is the output of this code snippet?
Pandas
import numpy as np import pandas as pd arr = np.array([10, 20, 30, 40]) series = pd.Series(arr) print(series)
Attempts:
2 left
💡 Hint
Think about how pandas.Series displays numpy arrays with default index.
✗ Incorrect
When a NumPy array is passed to pd.Series, it creates a Series with default integer index starting at 0. The output shows the index and values with dtype.
❓ data_output
intermediate1:30remaining
Shape of NumPy array from Pandas DataFrame values
What is the shape of the NumPy array obtained from the DataFrame values below?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]}) arr = df.values print(arr.shape)
Attempts:
2 left
💡 Hint
Remember rows and columns in DataFrame translate to shape (rows, columns).
✗ Incorrect
The DataFrame has 2 rows and 3 columns, so the NumPy array shape is (2, 3).
🔧 Debug
advanced2:00remaining
Error when assigning NumPy array to DataFrame column
What error does this code raise?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'X': [1, 2, 3]}) arr = np.array([4, 5]) df['Y'] = arr
Attempts:
2 left
💡 Hint
Check if the length of the array matches the DataFrame rows.
✗ Incorrect
Assigning a NumPy array with length 2 to a DataFrame column with 3 rows causes a ValueError due to length mismatch.
❓ visualization
advanced2:30remaining
Plotting NumPy data from Pandas DataFrame
Which option produces a line plot of the 'values' column from the DataFrame?
Pandas
import pandas as pd import numpy as np import matplotlib.pyplot as plt data = {'values': np.array([1, 3, 2, 5, 4])} df = pd.DataFrame(data) # Plot code here
Attempts:
2 left
💡 Hint
Use the DataFrame column directly for plotting.
✗ Incorrect
Plotting df['values'] directly produces a line plot with index on x-axis and values on y-axis. Other options either plot entire array or cause errors.
🧠 Conceptual
expert2:30remaining
Effect of modifying NumPy array on Pandas DataFrame
Given this code, what is the value of df.loc[0, 'A'] after modifying arr[0]?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'A': [10, 20, 30]}) arr = df.values arr[0, 0] = 99 print(df.loc[0, 'A'])
Attempts:
2 left
💡 Hint
Consider if df.values returns a view or a copy of the data.
✗ Incorrect
df.values returns a view of the underlying data. Modifying arr modifies the DataFrame data, so df.loc[0, 'A'] becomes 99.