在 Pandas 中,可以使用 DataFrame.columns 属性获取所有的列索引。例如:

import pandas as pd

df = pd.read_csv('data.csv')
column_indexes = df.columns
print(column_indexes)

输出结果为:

Index(['column1', 'column2', 'column3'], dtype='object')

其中 Index 对象就是包含所有列索引的对象。如果需要将其转换为列表,可以使用 tolist() 方法,例如:

column_indexes_list = df.columns.tolist()
print(column_indexes_list)

输出结果为:

['column1', 'column2', 'column3']
python获得列的所有索引

原文地址: https://www.cveoy.top/t/topic/fX3A 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录