loc and iloc in Pandas

PHOTO EMBED

Thu Nov 25 2021 06:55:14 GMT+0000 (Coordinated Universal Time)

Saved by @Sourabh ##dictionary ##pandas #defining_data_frame #csv #dataframe #square #bracket'

# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Print out observation for Japan
print(cars.loc['JPN'])
print(cars.iloc[2])

# Print out observations for Australia and Egypt
print(cars.loc[['AUS','EG']])
print(cars.iloc[[1,6]])
content_copyCOPY

With loc and iloc you can do practically any data selection operation on DataFrames you can think of. loc is label-based, which means that you have to specify rows and columns based on their row and column labels. iloc is integer index based, so you have to specify rows and columns by their integer index