Pandas index column title or name
To set the name of the index column in a pandas DataFrame, you can use the .rename_axis() method or the .index.name attribute.
To set the name of the index column in a pandas DataFrame, you can use the .rename_axis() method or the .index.name attribute. Here is an example:
set the name of the index column in a pandas DataFrame
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Method 1: using .rename_axis()
df = df.rename_axis("Index", axis=0)
# Method 2: using .index.name
df.index.name = "Index"
print(df)
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
This will set the name of the index column to "Index". You can replace "Index" with any other string to set the desired name.