【问题标题】:How to list the index and columns names together for a given dataframe?如何将给定数据框的索引和列名称一起列出?
【发布时间】:2021-03-06 21:24:41
【问题描述】:

如何在 python 的数据框中列出列名及其索引?

下面的代码只给出了列名的索引号。但是我需要学习如何为具有多个列名的大型数据集列出索引号以及列名。

enter code here:

columnnames=['a','b','c']
df.columns.get_loc(col) for col in  column_names

【问题讨论】:

标签: python python-3.x pandas dataframe indexing


【解决方案1】:

您可以像这样创建map

In [3359]: col_map = {df.columns.get_loc(c):c  for c in df.columns}

In [3360]: col_map
Out[3360]: {0: 'id', 1: 'count', 2: 'colors'}

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2014-08-31
    相关资源
    最近更新 更多