You can change the index in a Pandas DataFrame using the set_index()
method. This method allows you to specify a column or a list of columns to use as the new index.
Here's a breakdown of how to change the index:
1. Setting a New Index
-
Using a single column:
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 28], 'City': ['New York', 'London', 'Paris']} df = pd.DataFrame(data) df = df.set_index('Name') print(df)
This code sets the 'Name' column as the new index.
-
Using multiple columns:
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 28], 'City': ['New York', 'London', 'Paris']} df = pd.DataFrame(data) df = df.set_index(['Name', 'City']) print(df)
This code sets both 'Name' and 'City' columns as the new index, creating a hierarchical index.
2. Resetting the Index
If you want to revert to a default numerical index, use the reset_index()
method:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 28],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
df = df.set_index('Name')
df = df.reset_index()
print(df)
This will create a new numerical index, starting from 0, and move the previous index to a new column named 'Name'.
3. Renaming the Index
You can rename the index using the rename()
method:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 28],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
df = df.set_index('Name')
df = df.rename(index={'Alice': 'A', 'Bob': 'B', 'Charlie': 'C'})
print(df)
This code renames the index values 'Alice', 'Bob', and 'Charlie' to 'A', 'B', and 'C', respectively.
4. Setting a Custom Index
You can create a custom index using a list or a range:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 28],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
df = df.set_index(pd.Index(['A', 'B', 'C']))
print(df)
This code sets a custom index with values 'A', 'B', and 'C'.
Remember that changing the index can affect how you access and manipulate data in your DataFrame.