0
0
Data Analysis Pythondata~10 mins

Binning continuous variables in Data Analysis Python - 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 bins using pandas cut function.

Data Analysis Python
import pandas as pd

ages = [22, 45, 18, 34, 65, 27]
bins = [0, 18, 35, 60, 100]
categories = pd.cut(ages, [1])
print(categories)
Drag options to blanks, or click blank then click option'
Abins
Bages
Crange
Dcategories
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the data itself instead of the bins list.
Using a variable that is not defined as bins.
2fill in blank
medium

Complete the code to assign labels to the bins.

Data Analysis Python
import pandas as pd

ages = [22, 45, 18, 34, 65, 27]
bins = [0, 18, 35, 60, 100]
labels = ['Child', 'Young Adult', 'Adult', 'Senior']
categories = pd.cut(ages, bins, [1]=labels)
print(categories)
Drag options to blanks, or click blank then click option'
Abins
Bnames
Ccategories
Dlabels
Attempts:
3 left
💡 Hint
Common Mistakes
Using names instead of labels.
Passing the labels list as the first argument.
3fill in blank
hard

Fix the error in the code to create equal-width bins.

Data Analysis Python
import pandas as pd

ages = [22, 45, 18, 34, 65, 27]
categories = pd.cut(ages, bins=4, [1]=True)
print(categories)
Drag options to blanks, or click blank then click option'
Ainclude_lowest
Bright
Clabels
Dretbins
Attempts:
3 left
💡 Hint
Common Mistakes
Using right=True which controls bin edge inclusion on the right side.
Using labels when not assigning labels.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

Data Analysis Python
words = ['data', 'science', 'is', 'fun']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for the value.
Using < instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 2 characters.

Data Analysis Python
words = ['data', 'science', 'is', 'fun']
result = { [1]: [2] for word in words if len(word) [3] 2 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of word.upper() for keys.
Using < instead of > in the condition.