iloc[] Basics
You can also access rows in a DataFrame by their index. There are multiple ways to do this:
.iloc- is used to access rows by their numerical index, starting from 0;.loc- is used to access rows by their string label.
In this course, you will focus exclusively on using the .iloc attribute.
12345import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) print(countries)
The DataFrame has the following structure:
You can see the first column, which serves as the row index. Use these indexes to access specific rows in the DataFrame. The syntax of this attribute is:
df.iloc[index]
Use this attribute to access the third and seventh rows in the DataFrame:
12345678import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) # Accessing to the third and seventh rows print(countries.iloc[2]) print(countries.iloc[6])
After running the above code, you'll get rows that correspond to the indexes indicated in the image below:
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the difference between `.iloc` and `.loc` again?
How do I access multiple rows at once using `.iloc`?
What happens if I use an index that is out of range with `.iloc`?
Awesome!
Completion rate improved to 3.03
iloc[] Basics
Swipe to show menu
You can also access rows in a DataFrame by their index. There are multiple ways to do this:
.iloc- is used to access rows by their numerical index, starting from 0;.loc- is used to access rows by their string label.
In this course, you will focus exclusively on using the .iloc attribute.
12345import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) print(countries)
The DataFrame has the following structure:
You can see the first column, which serves as the row index. Use these indexes to access specific rows in the DataFrame. The syntax of this attribute is:
df.iloc[index]
Use this attribute to access the third and seventh rows in the DataFrame:
12345678import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) # Accessing to the third and seventh rows print(countries.iloc[2]) print(countries.iloc[6])
After running the above code, you'll get rows that correspond to the indexes indicated in the image below:
Thanks for your feedback!