Question
Grouping Data Task: Let's see if we have users with more than 50 song listens a day. To find out, first we need to devise
Grouping Data Task: Let's see if we have users with more than 50 song listens a day. To find out, first we need to devise a general tool that we can apply to our situation. Write a function, get_heavy_listener, which accepts a certain grouping as its groups argument. The function should sift through the entire grouping. Read on for details: There are two elements in each group: a group name and a selected part of the original DataFrame. You'll see why we need groups in the next task. Here, we want our function to loop through a group and do something with two elements each time. To do so, we'll write for group_name, group_data in groups that means that we expect the passed value (group) to have two values inside. We use the arbitrary variable names group_name (in the next task, this variable will represent users) and group_data to address them. When the function finds a group in which the selected part of the original DataFrame contains more than 50 rows (i.e. the length of this part of the data is greater than 50; in our case, that would mean that a certain user had more than 50 listens), it returns the group name. After it finds one such group, it terminates without looking any further. In this task, we will have no output, but the checker will verify if your code is correct. Hint: Use len() to find the length of group_data. Code:
import pandas as pd df = pd.read_csv('/datasets/music_log_upd_en.csv')
def get_heavy_listener(groups): for group_name, group_data in groups: if # if the length of group_data is greater than 50, then user = # save the element group_name to the variable user return user # just create the function, nothing else
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started