如何在 pandas 中遍历包括索引列在内的列名
只想遍历你的 DataFrame 的列名***不包括索引列?***请参见如何在 pandas 中遍历列名!
为了遍历所有列名包括索引列名,使用
include_index.py
for column_name in [df.index.name] + list(df.columns):
print(column_name)例如,我们可以打印 TechOverflow 时间序列示例数据集 的所有列名(包括索引列):
include_index_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)
for column_name in [df.index.name] + list(df.columns):
print(column_name)将打印
include_index_output.txt
Timestamp
Sine
CosineIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow