如何在 pandas 中遍历列名
为了在 pandas 中遍历列名,使用
how-to-iterate-column-names-in-pandas.py
for column in df.columns:
print(columns)例如,对于 TechOverflow pandas 时间序列示例数据集,df.columns 将是
columns_index_example.py
Index(['Sine', 'Cosine'], dtype='object')因此使用以下命令遍历列
iterate_columns_example.py
# 加载预构建的时间序列示例数据集
df = pd.read_csv("https://datasets.techoverflow.net/timeseries-example.csv", parse_dates=["Timestamp"])
df.set_index("Timestamp", inplace=True)
# 遍历列
for column in df.columns:
print(column)将打印
iterate_columns_output.txt
Sine
Cosine注意 df.columns 将不显示 索引列!
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow