Introduction to Pandas Indexing - ( Pandas Indexing )
Heading h2
Syntax
DataFrame.loc[row_indexer, column_indexer]
Example
import pandas as pd
data = {'name': ['Alice', 'Bob', 'Charlie', 'David'],
'age': [25, 30, 28, 18],
'gender': ['F', 'M', 'M', 'M']}
df = pd.DataFrame(data)
# selecting a single row using label-based indexing
row = df.loc[1]
print(row)
# selecting a subset of rows and columns using label-based indexing
subset = df.loc[[0,2], ['name', 'age']]
print(subset)
Output
name Bob
age 30
gender M
Name: 1, dtype: object
name age
0 Alice 25
2 Charlie 28
Explanation
Pandas indexing is the process of selecting or setting values in a DataFrame using different indexing methods such as label-based (.loc[]
) or integer-based (.iloc[]
) indexing.
.loc[]
is a label-based indexing method in Pandas that is used to select rows and columns by their labels. It accepts row and column indexers as input and returns a subset of the original DataFrame based on the given indexers.
In the above example, we create a simple DataFrame using a Python dictionary and then select a single row and a subset of rows and columns using .loc[]
indexing. In the first example, we select the second row of the DataFrame with label 1
using label-based indexing. In the second example, we select the first and third rows and the first two columns of the DataFrame using label-based indexing.
Use
Pandas indexing is a fundamental concept in data analysis and is used extensively in data manipulation and transformation. .loc[]
indexing is particularly useful when working with labeled data where the rows and columns have meaningful labels.
Important Points
- Pandas indexing is used to select or set values in a DataFrame
.loc[]
is a label-based indexing method in Pandas- It is used to select rows and columns by their labels
.loc[]
indexing returns a subset of the original DataFrame based on the given indexers
Summary
In conclusion, Pandas indexing is a fundamental concept in data analysis and is used extensively in data manipulation and transformation. .loc[]
indexing is a label-based indexing method that is used to select rows and columns by their labels. It is particularly useful when working with labeled data where the rows and columns have meaningful labels.