Using the axis parameter in pandas
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]:
In [6]:
# let's remove "continent" column
# axis=1 drops the column
drinks.drop('continent', axis=1).head()
Out[6]:
In [7]:
# drops second row
# axis=0 drops the row
drinks.drop(2, axis=0).head()
Out[7]:
In [8]:
# drops multiple rows
drop_rows = [0, 1]
drinks.drop(drop_rows, axis=0).head()
Out[8]:
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]:
In [12]:
# mean of each row
# drinks.mean(axis='columns)
drinks.mean(axis=1).head()
Out[12]:
In [14]:
drinks.mean(axis='index').head()
# this is the same as
# drinks.mean(axis=0).head()
Out[14]: