Using the axis parameter in pandas
Using "axis" Parameter

This introduction to pandas is derived from Data School's pandas Q&A with my own notes and code.

Using the "axis" parameter in pandas

In [1]:
import pandas as pd
In [4]:
url = 'http://bit.ly/drinksbycountry'
drinks = pd.read_csv(url)
In [5]:
drinks.head()
Out[5]:
country beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent
0 Afghanistan 0 0 0 0.0 Asia
1 Albania 89 132 54 4.9 Europe
2 Algeria 25 0 14 0.7 Africa
3 Andorra 245 138 312 12.4 Europe
4 Angola 217 57 45 5.9 Africa
In [6]:
# let's remove "continent" column
# axis=1 drops the column
drinks.drop('continent', axis=1).head()
Out[6]:
country beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol
0 Afghanistan 0 0 0 0.0
1 Albania 89 132 54 4.9
2 Algeria 25 0 14 0.7
3 Andorra 245 138 312 12.4
4 Angola 217 57 45 5.9
In [7]:
# drops second row
# axis=0 drops the row
drinks.drop(2, axis=0).head()
Out[7]:
country beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent
0 Afghanistan 0 0 0 0.0 Asia
1 Albania 89 132 54 4.9 Europe
3 Andorra 245 138 312 12.4 Europe
4 Angola 217 57 45 5.9 Africa
5 Antigua & Barbuda 102 128 45 4.9 North America
In [8]:
# drops multiple rows
drop_rows = [0, 1]
drinks.drop(drop_rows, axis=0).head()
Out[8]:
country beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent
2 Algeria 25 0 14 0.7 Africa
3 Andorra 245 138 312 12.4 Europe
4 Angola 217 57 45 5.9 Africa
5 Antigua & Barbuda 102 128 45 4.9 North America
6 Argentina 193 25 221 8.3 South America
In [10]:
# mean of each numeric column
drinks.mean()

# it is the same as the following command as axis=0 is the default
# drinks.mean(axis=0)
# it instructs pandas to move vertically
Out[10]:
beer_servings                   106.160622
spirit_servings                  80.994819
wine_servings                    49.450777
total_litres_of_pure_alcohol      4.717098
dtype: float64
In [12]:
# mean of each row
# drinks.mean(axis='columns)
drinks.mean(axis=1).head()
Out[12]:
0      0.000
1     69.975
2      9.925
3    176.850
4     81.225
dtype: float64
In [14]:
drinks.mean(axis='index').head()
# this is the same as 
# drinks.mean(axis=0).head()
Out[14]:
beer_servings                   106.160622
spirit_servings                  80.994819
wine_servings                    49.450777
total_litres_of_pure_alcohol      4.717098
dtype: float64
Tags: pandas