- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How are iloc and loc different in Python Pandas?
Let's take an example to understand the difference between iloc and loc. Basically loc[0] returns the value present at 0 index, whereas iloc[0] returns the value present at the first location of a series.
Steps
Create a one-dimensional ndarray with axis labels (including time series).
Print the input series.
Use loc[0] to print the value present at 0th index.
Use iloc[0] to print the value present at the first location of the series table.
Example
import pandas as pd s = pd.Series(list("AEIOU"), index=[2, 1, 0, 5, 8]) print "Input series is:
", s print "Value at index=0:", s.loc[0] print "Value at the 1st location of the series:", s.iloc[0]
Output
Input series is: 2 A 1 E 0 I 5 O 8 U dtype: object Value at index=0: I Value at the 1st location of the series: A
Advertisements