Apr-26-2017, 07:28 PM
I am having trouble understanding the following code. Note, auto is a DataFrame that contains the variables yr (year) and mpg (miles per gallon):
What is confusing me is the line "avg=group['mpg'].mean()." From what I've learned about indexing, I've never seen a value being able to be used at the beginning of the index. Since group is the value in the key value pairs, how does Python know that group['mpg'] refers to the mpg column?
splitting=auto.groupby(‘yr’) type(splitting.groups) for group_name, group in splitting: avg=group[‘mpg’].mean() print(group_name,avg)Here is what I understand: we are saving a groupby object to "splitting" that is grouped by year. Next, we see that the type of splitting.groups is a dictionary. We iterate over the key value pairs in splitting, obtain an average, and print the key along with it's average mpg.
What is confusing me is the line "avg=group['mpg'].mean()." From what I've learned about indexing, I've never seen a value being able to be used at the beginning of the index. Since group is the value in the key value pairs, how does Python know that group['mpg'] refers to the mpg column?