In [1]:
import pandas as pd
In [2]:
#Read the csv file
titanic_df = pd.read_csv('titanic.csv')
#It's a big file so let's extract a small data out of it
df = titanic_df.loc[[0,1,2,3,4,5],['name','sex','age','fare']]
df
Out[2]:
In [3]:
#Let's just print the name
df.name
Out[3]:
In [4]:
#we can print the name using loc also
df.loc[:,'name']
Out[4]:
In [5]:
#iloc can be used with positional integers
#First two rows and all columns
df.iloc[0:2,:]
Out[5]:
In [6]:
#all rows and last column
df.iloc[:,-1]
Out[6]:
In [7]:
#show the index
df.index
Out[7]:
In [8]:
#using criteria to filter
df[df.sex == 'female']
Out[8]:
In [9]:
df[df.index == 2]
Out[9]:
In [10]:
#we can use conditions
df[(df.sex == 'female') & (df.age >= 20)]
Out[10]: