首页 > 解决方案 > pandas.df.columns - making output for visually useful

问题描述

I'm working with a pandas df. Simple question, I hope:

pandas.df.columns returns an index with the column names of the df, separated by columns in a list. Example: Index(['a, 'c, 'c, 'c', 'd', 'e', 'f','g', 'h'],dtype='object')

I'd like the above in a vertical output (a would row 1, b row 2, etc.)

QUESTION: Is there more visually appealing way to return a list of column names, for instance with each column name in a separate row?

e.g. maybe each column name in a row vs separated by a comma in a run on list? Thanks.

Basically: something like pandas.df.dtypes but without the data types in the second column. I'd like it to return 'a' on row 1, 'b' on row 2, etc. I just want to the output to be more visually presentable vs everything running together. I hope that make sense. Thank you.

标签: pythonpandas

解决方案


When you add .values to df.columns you get a numpy ndarray, which gives you the option of iterating over its positions with a for loop.

for col in df.columns.values:
    print(col)

推荐阅读