python - 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.
解决方案
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)
推荐阅读
- python - 使用回溯和递归找到允许在迷宫中破墙的老鼠的最短路径。我的代码有什么问题?
- url - 无法从公司 VPN 浏览托管在 EC2 上的公共 URL?
- linux - Triggering long running operations from within a Linux ISR
- python - 增加子图的 y 刻度标签时 matplotlib 轴标签丢失
- c - Getting variable size in C preprocessor
- bootstrap-4 - Bootstrap 4 and Bootbox body horizontal scroll?
- javascript - time isn't match exact starttime
- amazon-web-services - What is the correct way to SSH into a AWS Windows 10 machine from the Windows 10 Command Line?
- android - Just toggle flash with CameraX
- javascript - Remove string sign from array element