十套练习使用pandas数据分析.3
import pandas as pd
drinks = pd. read_csv( "D:/数据分析/pandas_exercise/exercise_data/drinks.csv" )
drinks. head( )
country beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent 0 Afghanistan 0 0 0 0.0 AS 1 Albania 89 132 54 4.9 EU 2 Algeria 25 0 14 0.7 AF 3 Andorra 245 138 312 12.4 EU 4 Angola 217 57 45 5.9 AF
drinks. groupby( "continent" ) . beer_servings. mean( )
continent
AF 61.471698
AS 37.045455
EU 193.777778
OC 89.687500
SA 175.083333
Name: beer_servings, dtype: float64
drinks. groupby( "continent" ) . wine_servings. describe( )
count mean std min 25% 50% 75% max continent AF 53.0 16.264151 38.846419 0.0 1.0 2.0 13.00 233.0 AS 44.0 9.068182 21.667034 0.0 0.0 1.0 8.00 123.0 EU 45.0 142.222222 97.421738 0.0 59.0 128.0 195.00 370.0 OC 16.0 35.625000 64.555790 0.0 1.0 8.5 23.25 212.0 SA 12.0 62.416667 88.620189 1.0 3.0 12.0 98.50 221.0
drinks. groupby( 'continent' ) [ "beer_servings" , "spirit_servings" , "wine_servings" , "total_litres_of_pure_alcohol" ] . mean( )
C:\WINDOWS\TEMP/ipykernel_736/3320151383.py:1: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.
drinks.groupby('continent')["beer_servings","spirit_servings","wine_servings","total_litres_of_pure_alcohol"].mean()
beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent AF 61.471698 16.339623 16.264151 3.007547 AS 37.045455 60.840909 9.068182 2.170455 EU 193.777778 132.555556 142.222222 8.617778 OC 89.687500 58.437500 35.625000 3.381250 SA 175.083333 114.750000 62.416667 6.308333
drinks. groupby( 'continent' ) . mean( )
beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent AF 61.471698 16.339623 16.264151 3.007547 AS 37.045455 60.840909 9.068182 2.170455 EU 193.777778 132.555556 142.222222 8.617778 OC 89.687500 58.437500 35.625000 3.381250 SA 175.083333 114.750000 62.416667 6.308333
drinks. groupby( 'continent' ) . median( )
beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent AF 32.0 3.0 2.0 2.30 AS 17.5 16.0 1.0 1.20 EU 219.0 122.0 128.0 10.00 OC 52.5 37.0 8.5 1.75 SA 162.5 108.5 12.0 6.85
drinks. groupby( 'continent' ) [ "spirit_servings" ] . agg( [ "mean" , "max" , "min" ] )
mean max min continent AF 16.339623 152 0 AS 60.840909 326 0 EU 132.555556 373 0 OC 58.437500 254 0 SA 114.750000 302 25