0
0
Pandasdata~10 mins

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

Pandas
import pandas as pd

data = [5, 10, 15, 20, 25]
bins = pd.cut(data, [1])
print(bins)
Drag options to blanks, or click blank then click option'
A3
B[3, 6, 9]
C5
Dpd.qcut
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list of numbers instead of the number of bins.
Using qcut instead of cut for equal-width bins.
2fill in blank
medium

Complete the code to create 4 bins with equal number of data points using qcut().

Pandas
import pandas as pd

data = [5, 10, 15, 20, 25, 30, 35, 40]
bins = pd.[1](data, 4)
print(bins)
Drag options to blanks, or click blank then click option'
Acut
Bqcut
Cbin
Dgroupby
Attempts:
3 left
💡 Hint
Common Mistakes
Using cut() which creates equal-width bins, not equal-sized bins.
Using incorrect function names like bin or groupby.
3fill in blank
hard

Fix the error in the code to create bins with cut() using custom bin edges.

Pandas
import pandas as pd

values = [1, 2, 3, 4, 5]
bins = pd.cut(values, bins=[1])
print(bins)
Drag options to blanks, or click blank then click option'
Apd.qcut
B3
C[0, 2, 4, 6]
D['a', 'b', 'c']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single integer instead of a list for bins.
Passing non-numeric bin edges.
4fill in blank
hard

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

Pandas
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)
Bword
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length as the value.
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.

Pandas
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
result = { [1]: [2] for word in words if len(word) [3] 4 }
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 the original word instead of uppercase for keys.
Using '<' or '<=' instead of '>' in the condition.