如何在 pandas 中获取索引列的名称

为了获取 pandas DataFrame 的索引列名称,使用

index_name.py
df.index.name

例如,我们可以打印 TechOverflow 时间序列示例数据集 的索引列名称:

index_name_example.py
import pandas as pd

# 加载预构建的时间序列示例数据集
df = pd.read_csv("https://datasets.techoverflow.net/timeseries-example.csv", parse_dates=["Timestamp"])
df.set_index("Timestamp", inplace=True)

print(df.index.name)

将打印

index_name_output.txt
Timestamp

Check out similar posts by category: Pandas, Python