Python pandas DataFrame - Tutorial 2
Group by
pandas has 'groupby' function that it will group the data by any categorical values, and it can be used with numerical calculations such as mean, sum, etc.
I calculated the average runtime and domestic total gross for each rating. You need to be cautious that if you have more than one numerical column to be calculated, you need double brackets for the columns.
![](https://static.wixstatic.com/media/ec25b8_15d20f56fcf341fe8e688da99f62e16c~mv2.png/v1/fill/w_679,h_183,al_c,q_85,enc_auto/ec25b8_15d20f56fcf341fe8e688da99f62e16c~mv2.png)
Pandas is smart enough to calculate only for the numerical columns even though you don't specify the columns. Check an example below.
![](https://static.wixstatic.com/media/ec25b8_b0a263c8f29f4ce1a560b1de380a12a4~mv2.png/v1/fill/w_378,h_183,al_c,q_85,enc_auto/ec25b8_b0a263c8f29f4ce1a560b1de380a12a4~mv2.png)
I was able to calculate the total values for each rating without specifying what columns are included for the sum, and pandas calculated the sum only for the numerical values.
'Rating' column will be used as index if 'as_index=False' is not included for groupby.
Filter by column values
When you look for column values, pandas return True or False for each row. You can use True and False to filter rows by the column values.
![](https://static.wixstatic.com/media/ec25b8_cbff58df640b43199c4b879661ef54d5~mv2.png/v1/fill/w_192,h_226,al_c,q_85,enc_auto/ec25b8_cbff58df640b43199c4b879661ef54d5~mv2.png)
df.loc selects rows by lable, and df.iloc selects rows by position. 'loc' and 'iloc' receive the True and False values and return only True rows.
![](https://static.wixstatic.com/media/ec25b8_8df30e0a1f494d1b817c1f79900f40df~mv2.png/v1/fill/w_947,h_264,al_c,q_85,enc_auto/ec25b8_8df30e0a1f494d1b817c1f79900f40df~mv2.png)
Only PG-13 movies are filter. Index number is not consecutive, since only it displays PG-13 movies. It does not reset index number.
Q: Find a director who had the highest domestic total gross.
![](https://static.wixstatic.com/media/ec25b8_bb4ab550cb3c40f9b58e4f65e057ba46~mv2.png/v1/fill/w_604,h_74,al_c,q_85,enc_auto/ec25b8_bb4ab550cb3c40f9b58e4f65e057ba46~mv2.png)
df.DomesticTotalGross.max() returns the highest value in DomesticTotalGross column.
df.loc[. . . . .].Director will return the value in Director column for the row selected with df.loc.
As a practical use for filter, here is a final example.
# Label for ratings. This variable will be used in plt plot right below. ratings=['G','PG','PG-13','R']
plt.figure(figsize=(20,20)) for i in range(len(df.Rating.unique())): plt.subplot(4,1,i+1) plt.plot_date(df.loc[df.Rating == ratings[i]].date, df.loc[df.Rating == ratings[i]].DomesticTotalGross, label=ratings[i]) plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.legend(fontsize=25,loc='upper left')
plt.tight_layout()
![](https://static.wixstatic.com/media/ec25b8_e4055e730bd0404c9d1b96a1cedb9ee7~mv2_d_1440_1440_s_2.png/v1/fill/w_980,h_980,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/ec25b8_e4055e730bd0404c9d1b96a1cedb9ee7~mv2_d_1440_1440_s_2.png)