- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 returns the value present at 0 index, whereas iloc returns the value present at the first location of a series.
Create a one-dimensional ndarray with axis labels (including time series).
Print the input series.
Use loc to print the value present at 0th index.
Use iloc to print the value present at the first location of the series table.
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 print "Value at the 1st location of the series:", s.iloc
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
- Related Articles
- Extracting rows using Pandas .iloc in Python
- How to append a list to a Pandas DataFrame using loc in Python?
- How to append a list to a Pandas DataFrame using iloc in Python?
- How to access pandas DataFrame elements using the .loc attribute?
- How to access pandas Series elements using the .loc attribute?
- How to access pandas DataFrame elements using the .iloc attribute?
- How to access pandas Series elements using the .iloc attribute?
- How to access a group of elements from pandas Series using the .iloc attribute with slicing object?
- What are the different ways to install pandas?
- What are stack and unstack functions in the Python Pandas library.
- What are different basic operators in Python?
- What are different arithmetic operators in Python?
- How are dataframes in Pandas merged?
- How are iOS and Android similar? How are they different?
- What are different data conversion methods in Python?