How to iterate column names including the index column in pandas
Just want to iterate over your DataFrame’s column names ***without the index column?***See How to iterate column names in pandas!
In order to iterate over all column names including the index column name, use
for column_name in [df.index.name] + list(df.columns):
print(column_name)
For example, we can print the name of all columns including the index column of the TechOverflow time series example dataset:
import pandas as pd
# Load pre-built time series example dataset
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)
which will print
Timestamp
Sine
Cosine