Pandas set_index() method is used to set the DataFrame index (row labels) using one or more existing columns. It can replace the existing index or expand it.
Syntax
DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)
Parameters
Name | Description |
keys | Column label or list of column labels to be set as index. |
drop | If True, the column(s) used as a new index will be removed from the DataFrame; if False, they will remain in the DataFrame. |
append | If True, append columns to the existing index; if False, replace the existing index. |
inplace | If True, modify the DataFrame in place; if False, return a new DataFrame. |
verify_integrity | If True, check for duplicates in the new index; if False, no check is performed. |
Return value
It returns a new DataFrame with the set index if inplace=False or None if inplace=True.
Example 1: Setting a single column as an index
import pandas as pd
df = pd.DataFrame({'A': ['a', 'b', 'c'],
'B': [1, 2, 3],
'C': [4, 5, 6]})
df.set_index('A', inplace=True)
print(df)
Output
Example 2: Setting multiple columns as an index
import pandas as pd
df = pd.DataFrame({'A': ['a', 'b', 'c'],
'B': [1, 2, 3],
'C': [4, 5, 6]})
df.set_index(['A', 'B'], inplace=True)
print(df)
Output
Example 3: Keeping the column in DataFrame after setting it as an index
import pandas as pd
df = pd.DataFrame({'A': ['a', 'b', 'c'],
'B': [1, 2, 3],
'C': [4, 5, 6]})
df.set_index('A', drop=False, inplace=True)
print(df)
Output
Example 4: Appending to an existing index
import pandas as pd
df = pd.DataFrame({'A': ['a', 'b', 'c'],
'B': [1, 2, 3],
'C': [4, 5, 6]})
df.set_index('A', inplace=True)
df.set_index('B', append=True, inplace=True)
print(df)
Output
The set_index() with multiple columns creates a multi-level index (hierarchical index), which is extremely powerful for grouping and pivoting data.
The reset_index() method is complementary to set_index() and can revert the index to a column or create a default integer index.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Great share! very immpreive article