0
0
NumPydata~10 mins

NumPy with Pandas integration - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert a Pandas Series to a NumPy array.

NumPy
import pandas as pd
s = pd.Series([1, 2, 3, 4])
arr = s.[1]()
Drag options to blanks, or click blank then click option'
Aarray
Basarray
Cvalues
Dto_numpy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values' which is deprecated in newer Pandas versions.
Trying to use 'array' which is not a Series method.
2fill in blank
medium

Complete the code to create a Pandas DataFrame from a NumPy array.

NumPy
import numpy as np
import pandas as pd
arr = np.array([[1, 2], [3, 4]])
df = pd.DataFrame([1])
Drag options to blanks, or click blank then click option'
Aarr
B[arr]
Carr.tolist()
Darr.values
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list containing the array instead of the array itself.
Using 'arr.values' which is invalid for a NumPy array.
3fill in blank
hard

Fix the error in the code to get the mean of a NumPy array column in a DataFrame.

NumPy
import numpy as np
import pandas as pd
data = {'A': np.array([1, 2, 3]), 'B': [4, 5, 6]}
df = pd.DataFrame(data)
mean_col = df['A'].[1]()
Drag options to blanks, or click blank then click option'
Amean
Baverage
Cmean()
Davg
Attempts:
3 left
💡 Hint
Common Mistakes
Including parentheses inside the blank causing syntax error.
Using 'average' or 'avg' which are not valid Pandas methods.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if length is greater than 3.

NumPy
words = ['data', 'science', 'is', 'fun']
lengths = { [1] : [2] for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.upper()
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using transformations like upper or lower instead of the word itself.
Using the length as the key instead of the value.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their values only if value is greater than 0.

NumPy
data = {'a': 1, 'b': -1, 'c': 3}
result = { [1] : [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase keys instead of uppercase.
Using wrong comparison operators like '<' or '==' in the condition.
Swapping keys and values.