0
0
Data Analysis Pythondata~10 mins

cut() and qcut() for binning 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 3 equal-width bins from the data using cut().

Data Analysis Python
import pandas as pd
values = [5, 10, 15, 20, 25]
bins = pd.cut(values, [1])
print(bins)
Drag options to blanks, or click blank then click option'
A3
B[3]
C'3'
Drange(3)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing bins as a list or string instead of an integer.
Using range() which is not accepted directly.
2fill in blank
medium

Complete the code to create 4 quantile-based bins using qcut().

Data Analysis Python
import pandas as pd
values = [1, 2, 3, 4, 5, 6, 7, 8]
quantile_bins = pd.qcut(values, [1])
print(quantile_bins)
Drag options to blanks, or click blank then click option'
A3
B4
C5
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a number that does not divide data into equal quantiles.
Confusing cut() bins with qcut() quantiles.
3fill in blank
hard

Fix the error in the code to label bins with custom names using cut().

Data Analysis Python
import pandas as pd
values = [10, 20, 30, 40, 50]
labels = ['Low', 'Medium', 'High']
binned = pd.cut(values, bins=3, labels=[1])
print(binned)
Drag options to blanks, or click blank then click option'
A3
B['Low', 'Medium']
C['Low', 'Medium', 'High', 'Very High']
Dlabels
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list with wrong length for labels.
Passing an integer instead of a list for 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 = ['apple', 'bat', 'carrot', 'dog', 'elephant']
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 the word itself instead of its length.
Using '<' instead of '>' in the condition.
5fill in blank
hard

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

Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
result = {{ [1]: [2] for w in words if len(w) [3] 4 }}
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using '<' instead of '>' in the condition.
Using w instead of len(w) for values.