iloc in python syntax

iloc in python syntax : Pandas Tricks for you

Popular Domains for just 99 Cents at Namecheap!

iloc in python syntax is dataframe.iloc[row_num, col_num]. We use iloc in pandas for selecting rows on the basis of their index location.
Here the row_num and col_name may be a single value or a list as well. Well, In this article, We will see a different variations of iloc in python syntax.

iloc in python syntax :

Here is the syntax for iloc with example. Suppose, We want to extract row 1 from the dataframe. We can do it like below.

import pandas as pd
dict_data = {
"col1":[111,233,333,411],
"col2":[5131,623,7142,8431],
"col3":[13310,241030,88476,41400]}
#dataframe creation from dict
df = pd.DataFrame(data=dict_data)
#selecting row 1 with iloc
df.iloc[1]

"<yoastmark

It is showing row_1 with complete columns. Because we have not provided any criteria in iloc for column. But how can we achieve it ? We will see in the below sections.

iloc in python variation –

There may be so many variations in iloc python. We will see them one by one in the below sections.

1.iloc with indices list –

We can give the selection parameter as iloc as a list. Whether it is a column or a row.

import pandas as pd
dict_data = {
"col1":[111,233,333,411],
"col2":[5131,623,7142,8431],
"col3":[13310,241030,88476,41400]}
#dataframe creation from dict
df = pd.DataFrame(data=dict_data)
#selecting row 1 with iloc
df.iloc[[0,1],[0]]

Here we have used df.iloc[[0,1],[0]] where it will select the row_0 and row_1 and col_0. Let’s run and verify it.

iloc in python using list
iloc in python using list

2.iloc with Slicing indices –

If we need to select so many rows, We can not give them one by one. Hence we can give them in the form of the range. We can give it like this –

df.iloc[0:1,0:2]

Let’s run the complete code with the above part.

import pandas as pd
dict_data = {
"col1":[111,233,333,411],
"col2":[5131,623,7142,8431],
"col3":[13310,241030,88476,41400]}
#dataframe creation from dict
df = pd.DataFrame(data=dict_data)
#selecting row 1 with iloc
df.iloc[0:1,0:2]
iloc in python using slices
0 iloc in python using slices

 

In the above code, we have given the range for [0:1] in rows. That’s why it selected the row_0. In the same way, We have given the range for 0:2 in the column. Hence it selected the col1, col2.

Conclusion –

iloc is really important in dataframe row selection with pandas .  We have seen so many variations in iloc syntax. I hope now you may effectively use iloc. Well If you know some more variation in this syntax, Please let us know. You may also mention it in the below comment box.

 

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner