What is the difference between loc and iloc in Python?
LOC and ILOC are the two commonly used functions to subset data in a pandas data frame. Subsetting means selecting rows and columns based on the requirement.
ILOC:
It is a positional-based subsetting technique. Here we select rows and columns based on specific integer index positions. Its syntax is
dataframe.iloc[start_index, end_index -1]
For example in the below dataframe named ‘df_data’
df_data.iloc[1:3] will give the 1st and 2nd index positions row data
Now we will change the dataframe’s index to ‘Roll’
df_data.set_index(‘Roll’,drop=True, inplace=True)
df_data.iloc[1:3] will give the 1st and 2nd index positions row data
LOC:
It is a label-based subsetting technique. Here we select rows and columns based on the specified labels. Its syntax is
dataframe.loc[start_index, end_index]
For example in the below dataframe named ‘df_data’
df_data.loc[1:3] will give the 1st, 2nd, and 3rd index positions row data (Note: iloc only returned the 1st and 2nd row data)
Now we will change the dataframe’s index to ‘Roll’
df_data.set_index(‘Roll’,drop=True, inplace=True)
Now we get an error if we try to execute df_data.loc[1:3] as there is no row label data with the label as 3 after the third row (i.e label 1).
To get the first three rows we have to execute df_data.loc[43:1]