0
0
Pandasdata~10 mins

Pandas and NumPy connection - Interactive Code Practice

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

Complete the code to create a NumPy array from a pandas Series.

Pandas
import pandas as pd
import numpy as np

s = pd.Series([1, 2, 3, 4])
arr = s.[1]()
Drag options to blanks, or click blank then click option'
Aasarray
Bvalues
Carray
Dto_numpy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values' which is an attribute, not a method.
Using 'array' which is a NumPy function, not a pandas Series method.
2fill in blank
medium

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

Pandas
import pandas as pd
import numpy as np

arr = np.array([[1, 2], [3, 4]])
df = pd.[1](arr, columns=['A', 'B'])
Drag options to blanks, or click blank then click option'
ASeries
BDataFrame
CPanel
DArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Series' which creates a one-dimensional object.
Using 'Panel' which is deprecated and not for 2D arrays.
3fill in blank
hard

Fix the error in the code to convert a pandas DataFrame column to a NumPy array.

Pandas
import pandas as pd
import numpy as np

df = pd.DataFrame({'col': [10, 20, 30]})
arr = df['col'].[1]
Drag options to blanks, or click blank then click option'
Ato_numpy()
Bvalues()
Cto_list()
Darray()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values()' which causes an error because 'values' is an attribute.
Using 'to_list()' which returns a Python list, not a NumPy array.
4fill in blank
hard

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

Pandas
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' as the value instead of its length.
Using 'word > 3' which compares a string to a number.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 3.

Pandas
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for word in words if [3] }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 3
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.
Using word instead of len(word) for the value.