Pandas ( Indexing and Selecting).

Aman S
3 min readJul 2, 2022
Image made in Canve

Python DataFrame is a two-dimensional size-mutable, tabular data structure with labeled axes (rows and columns).

Pandas DataFrame consists of three principal components, the data, rows, and columns.

Image from GeeksforGeeks

Indexing in Pandas

Pandas have some operators for indexing.

loc and iloc.

We use these operations for some kind of advanced operations.

DATA-SET LINK,

We will be doing all the operations with this dataset.

I dropped the Unnamed column in the dataset for better understanding.

Import the Library.

import pandas as pd
df= pd.read_csv(“winemag-data-130k-v2.csv”)

loc deals with rows of the DataFrame.

iloc deals with columns of the DataFrame.

Let us print the first three rows of the DataFrame.

iloc

To select the first row of data in the DataFrame, we use the following.

df.iloc[0]

To get a column with iloc, we do the following.

df.iloc[ : , 0 ]

In the above code, we see operator : which means everything.

For example, if we want or get the first 5 rows of the first columns, we use

df.iloc[:5, 0]

To get the last five elements of the dataset.

df.iloc[-5 : ]

SELECTION

Label-based Selection

We use loc OPERATOR, In this, its deals with an index value, not its position, which matters.

For example, to get the first entry in DataFrame, we use the following.

df.loc[0,’country’]

loc or iloc

  • loc is faster, but you cant use arrays for indexers.
  • loc works only on the index, but iloc works on position.
  • iloc works on selecting the range of columns and rows simultaneously.
  • loc() is a label-based data selecting method which means that we have to pass the name of the row or column which we want to select.

LEARN WITH ME. ✌️. Whatever thing I learn, I document it.

Connect with me.

--

--