0
0
Pandasdata~10 mins

Numeric types (int64, float64) in Pandas - 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 pandas Series with integer type.

Pandas
import pandas as pd
s = pd.Series([1, 2, 3], dtype=[1])
print(s.dtype)
Drag options to blanks, or click blank then click option'
A"str"
B"float64"
C"int64"
D"bool"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'float64' instead of 'int64' for integer data.
Not specifying dtype at all.
Using 'str' which is for text data.
2fill in blank
medium

Complete the code to convert a pandas Series to float type.

Pandas
import pandas as pd
s = pd.Series([1, 2, 3])
s = s.astype([1])
print(s.dtype)
Drag options to blanks, or click blank then click option'
A"int32"
B"object"
C"bool"
D"float64"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int32' which keeps the type as integer.
Using 'bool' which converts to True/False values.
Using 'object' which is for mixed types or strings.
3fill in blank
hard

Fix the error in the code to create a DataFrame column with float type.

Pandas
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3]})
df['A'] = df['A'].astype([1])
print(df['A'].dtype)
Drag options to blanks, or click blank then click option'
A"float64"
B"category"
C"int64"
D"string"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int64' which does not change the type.
Using 'string' which is for text data.
Using 'category' which is for categorical data.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their length only if length is greater than 3.

Pandas
words = ['data', 'science', 'ai', 'ml']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dword == 'data'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using a condition that does not filter by length.
Confusing the order of key and value in the dictionary.
5fill in blank
hard

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

Pandas
words = ['cat', 'dog', 'a', 'bird']
result = { [1]: [2] for w in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 2
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Using the word itself instead of its length for values.
Incorrect condition that does not filter properly.