Manipulating Dataframes With Pandas PDF
Manipulating Dataframes With Pandas PDF
Manipulating
DataFrames
with pandas
Manipulating DataFrames with pandas
See you in
the course!
MANIPULATING DATAFRAMES WITH PANDAS
Indexing
DataFrames
Manipulating DataFrames with pandas
A simple DataFrame
In [1]: import pandas as pd
In [3]: df
Out[3]:
eggs salt spam
month
Jan 47 12.0 17
Feb 110 50.0 31
Mar 221 89.0 72
Apr 77 87.0 20
May 132 NaN 52
Jun 205 60.0 55
Manipulating DataFrames with pandas
In [5]: df['salt']['Jan']
Out[5]: 12.0
Manipulating DataFrames with pandas
In [7]: df.eggs['Mar']
Out[7]: 221
Manipulating DataFrames with pandas
In [11]: df.iloc[4, 2]
Out[11]: 52.0
Manipulating DataFrames with pandas
In [13]: df_new
Out[13]:
salt eggs
month
Jan 12.0 47
Feb 50.0 110
Mar 89.0 221
Apr 87.0 77
May NaN 132
Jun 60.0 205
MANIPULATING DATAFRAMES WITH PANDAS
Let’s practice!
MANIPULATING DATAFRAMES WITH PANDAS
Slicing DataFrames
Manipulating DataFrames with pandas
sales DataFrame
In [1]: df
Out[1]:
eggs salt spam
month
Jan 47 12.0 17
Feb 110 50.0 31
Mar 221 89.0 72
Apr 77 87.0 20
May 132 NaN 52
Jun 205 60.0 55
Manipulating DataFrames with pandas
In [3]: type(df['eggs'])
Out[3]: pandas.core.series.Series
Manipulating DataFrames with pandas
Using .iloc[]
In [9]: df.iloc[2:5, 1:] # A block from middle of the DataFrame
Out[9]:
salt spam
month
Mar 89.0 72
Apr 87.0 20
May NaN 52
Manipulating DataFrames with pandas
Let’s practice!
MANIPULATING DATAFRAMES WITH PANDAS
Filtering
DataFrames
Manipulating DataFrames with pandas
In [4]: df[enough_salt_sold]
Out[4]:
eggs salt spam
month
Mar 221 89.0 72
Apr 77 87.0 20
Manipulating DataFrames with pandas
Combining filters
In [5]: df[(df.salt >= 50) & (df.eggs < 200)] # Both conditions
Out[5]:
eggs salt spam
month
Feb 110 50.0 31
Apr 77 87.0 20
In [9]: df2
Out[9]:
eggs salt spam bacon
month
Jan 47 12.0 17 0
Feb 110 50.0 31 0
Mar 221 89.0 72 50
Apr 77 87.0 20 60
May 132 NaN 52 70
Jun 205 60.0 55 80
Manipulating DataFrames with pandas
In [17]: df
Out[17]:
eggs salt spam
month
Jan 47 12.0 17
Feb 110 50.0 31
Mar 226 89.0 72
Apr 82 87.0 20
May 132 NaN 52
Jun 210 60.0 55
MANIPULATING DATAFRAMES WITH PANDAS
Let’s practice!
MANIPULATING DATAFRAMES WITH PANDAS
Transforming
DataFrames
Manipulating DataFrames with pandas
Storing a transformation
In [7]: df['dozens_of_eggs'] = df.eggs.floordiv(12)
In [8]: df
Out[8]:
eggs salt spam dozens_of_eggs
month
Jan 47 12.0 17 3
Feb 110 50.0 31 9
Mar 221 89.0 72 18
Apr 77 87.0 20 6
May 132 NaN 52 11
Jun 205 60.0 55 17
Manipulating DataFrames with pandas
In [10]: df.index
Out[10]: Index(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
dtype='object', name='month')
Manipulating DataFrames with pandas
In [12]: df
Out[12]:
eggs salt spam dozens_of_eggs
month
JAN 47 12.0 17 3
FEB 110 50.0 31 9
MAR 221 89.0 72 18
APR 77 87.0 20 6
MAY 132 NaN 52 11
JUN 205 60.0 55 17
Manipulating DataFrames with pandas
In [14]: df
Out[14]:
eggs salt spam dozens_of_eggs
jan 47 12.0 17 3
feb 110 50.0 31 9
mar 221 89.0 72 18
apr 77 87.0 20 6
may 132 NaN 52 11
jun 205 60.0 55 17
Manipulating DataFrames with pandas
In [16]: df
Out[16]:
eggs salt spam dozens_of_eggs salty_eggs
jan 47 12.0 17 3 15.0
feb 110 50.0 31 9 59.0
mar 221 89.0 72 18 107.0
apr 77 87.0 20 6 93.0
may 132 NaN 52 11 NaN
jun 205 60.0 55 17 77.0
MANIPULATING DATAFRAMES WITH PANDAS
Let’s practice!