pandas
  1. pandas-set-index

Set Index - ( Pandas Indexing )

Heading h2

Syntax

DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)

Example

import pandas as pd

data = {'name': ['John', 'Jane', 'Fred', 'Samantha'],
       'age': [25, 30, 35, 40],
       'country': ['USA', 'UK', 'Canada', 'Australia']}

df = pd.DataFrame(data)

df.set_index('name', inplace=True)

print(df)

Output

          age    country
name                   
John       25        USA
Jane       30         UK
Fred       35     Canada
Samantha   40  Australia

Explanation

In Pandas, an index is a way to uniquely identify each row in a DataFrame or a Series. The set_index() function is used to set one or more columns as the index of a DataFrame. It takes the column(s) name as input and returns a new DataFrame with the specified column(s) as the index. The original DataFrame is not modified unless the inplace=True argument is used.

In the above example, the DataFrame df is indexed by the name column using the set_index() function. The inplace=True argument is used to modify the original DataFrame.

Use

Setting an index is important for performing various operations on DataFrames, such as filtering, merging, and grouping. A properly defined index can improve the performance of data selection and manipulation operations.

Important Points

  • An index is a way to uniquely identify each row in a DataFrame or a Series in Pandas.
  • The set_index() function is used to set one or more columns as the index of a DataFrame.
  • It takes the column(s) name as input and returns a new DataFrame with the specified column(s) as the index.
  • The original DataFrame is not modified unless the inplace=True argument is used.
  • A properly defined index can improve the performance of data selection and manipulation operations.

Summary

In conclusion, setting an index is an important step in data analysis using Pandas. The set_index() function is used to set one or more columns as the index of a DataFrame. A properly defined index can improve the performance of data selection and manipulation operations.

Published on: