Complete the code to create bins using pandas cut function.
import pandas as pd ages = [22, 45, 18, 34, 65, 27] bins = [0, 18, 35, 60, 100] categories = pd.cut(ages, [1]) print(categories)
The pd.cut function requires the bins argument to define the intervals. Here, bins is the list of bin edges.
Complete the code to assign labels to the bins.
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)
names instead of labels.The labels parameter in pd.cut assigns names to each bin interval.
Fix the error in the code to create equal-width bins.
import pandas as pd ages = [22, 45, 18, 34, 65, 27] categories = pd.cut(ages, bins=4, [1]=True) print(categories)
right=True which controls bin edge inclusion on the right side.labels when not assigning labels.The include_lowest=True parameter ensures the lowest value is included in the first bin.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
word instead of len(word) for the value.< instead of > in the condition.The dictionary comprehension maps each word to its length if the word length is greater than 3.
Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 2 characters.
words = ['data', 'science', 'is', 'fun'] result = { [1]: [2] for word in words if len(word) [3] 2 } print(result)
word instead of word.upper() for keys.< instead of > in the condition.The dictionary comprehension creates keys as uppercase words and values as their lengths, filtering words longer than 2 characters.