What is difference between iloc and loc in Pandas?
loc vs iloc:
The loc
indexer can also do boolean selection. For instance, if we are interested in finding all the rows where Age is less 30 and return just the Color and Height columns we can do the following. We can replicate this with iloc
but we cannot pass it a boolean series. We must convert the boolean Series into a numpy array.
loc
gets rows (or columns) with particular labels from the index.
iloc
gets rows (or columns) at particular positions in the index (so it only takes integers).
import pandas as pd df = pd.DataFrame({'Age': [30, 20, 22, 40, 32, 28, 39], 'Color': ['Blue', 'Green', 'Red', 'White', 'Gray', 'Black', 'Red'], 'Food': ['Steak', 'Lamb', 'Mango', 'Apple', 'Cheese', 'Melon', 'Beans'], 'Height': [165, 70, 120, 80, 180, 172, 150], 'Score': [4.6, 8.3, 9.0, 3.3, 1.8, 9.5, 2.2], 'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX'] }, index=['Jane', 'Nick', 'Aaron', 'Penelope', 'Dean', 'Christina', 'Cornelia']) print("\n -- loc -- \n") print(df.loc[df['Age'] < 30, ['Color', 'Height']]) print("\n -- iloc -- \n") print(df.iloc[(df['Age'] < 30).values, [1, 3]])
C:\python\pandas examples>python example14.py -- loc -- Color Height Nick Green 70 Aaron Red 120 Christina Black 172 -- iloc -- Color Height Nick Green 70 Aaron Red 120 Christina Black 172 C:\python\pandas examples>
2019-01-04T14:34:08+05:30
2019-01-04T14:34:08+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution