These two built-in functions in Pandas are used to slice a dataset. In order to that, I have used the titanic dataset available here. This is Titanic ML completion from Kaggle and it is mainly used for beginners to create a model that predicts which passengers survived during the Titanic shipwreck. First, lets briefly look at the dataset to see how many observations and columns it has.

Data

titanic.head()
titanic.info()

Image for post

Image for post

.loc function

This function is primarily label based, but also is used with a boolean array when we create statements. If we want to look at only rows for male customers here I would be able to use pandas.DataFrame.loc used for locating with labels.

titanic.loc[titanic[‘Sex’]==’male’]

Image for post

It provides me with a list of all male Passengers. We can do the same process for using every customer.

#pandas-dataframe #python3 #function

Loc and iLoc functions in Pandas
1.35 GEEK