How to iterate column names in pandas
In order to iterate column names in pandas
, use
for column in df.columns:
print(columns)
For example, for the TechOverflow pandas time series example dataset, df.columns
will be
Index(['Sine', 'Cosine'], dtype='object')
so iterating over the columns using
# 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)
# Iterate over columns
for column in df.columns:
print(column)
will print
Sine
Cosine
Note that df.columns
will notshow the index column!