How to iterate column names in pandas
In order to iterate column names in pandas, use
how-to-iterate-column-names-in-pandas.py
for column in df.columns:
print(columns)For example, for the TechOverflow pandas time series example dataset, df.columnswill be
columns_index_example.py
Index(['Sine', 'Cosine'], dtype='object')so iterating over the columns using
iterate_columns_example.py
# 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
iterate_columns_output.txt
Sine
CosineNote that df.columns will notshow the index column!
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow