pandas 获取python numpy数组的列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47588696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:51:01  来源:igfitidea点击:

Get the column names of a python numpy array

pythonarrayspandasnumpy

提问by ebrahimi

I have a csv data file with a header indicating the column names.

我有一个带有指示列名称的标题的 csv 数据文件。

xy   wz  hi kq
0    10  5  6
1    2   4  7
2    5   2  6

I run:

我跑:

X = np.array(pd.read_csv('gbk_X_1.csv').values)

I want to get the column names:

我想获取列名:

['xy', 'wz', 'hi', 'kg']

I read this postbut the solution provides me with None.

我阅读了这篇文章,但该解决方案为我提供了无。

采纳答案by Ahmad

Use the following code:

使用以下代码:

import re

f = open('f.csv','r')

alllines = f.readlines()
columns = re.sub(' +',' ',alllines[0]) #delete extra space in one line
columns = columns.strip().split(',') #split using space

print(columns)

Assume CSV file is like this:

假设 CSV 文件是这样的:

xy   wz  hi kq
0    10  5  6
1    2   4  7
2    5   2  6

回答by piRSquared

Let's assume your csv file looks like

让我们假设您的 csv 文件看起来像

xy,wz,hi,kq
0,10,5,6
1,2,4,7
2,5,2,6

Then use pd.read_csvto dump the file into a dataframe

然后用于pd.read_csv将文件转储到数据帧中

df = pd.read_csv('gbk_X_1.csv')

The dataframe now looks like

数据框现在看起来像

df

   xy  wz  hi  kq
0   0  10   5   6
1   1   2   4   7
2   2   5   2   6

It's three main components are the

它的三个主要组成部分是

  • datawhich you can access via the valuesattribute

    df.values
    
    array([[ 0, 10,  5,  6],
           [ 1,  2,  4,  7],
           [ 2,  5,  2,  6]])
    
  • indexwhich you can access via the indexattribute

    df.index
    
    RangeIndex(start=0, stop=3, step=1)
    
  • columnswhich you can access via the columnsattribute

    df.columns
    
    Index(['xy', 'wz', 'hi', 'kq'], dtype='object')
    
  • 数据您可以通过访问values属性

    df.values
    
    array([[ 0, 10,  5,  6],
           [ 1,  2,  4,  7],
           [ 2,  5,  2,  6]])
    
  • 您可以通过index属性访问的索引

    df.index
    
    RangeIndex(start=0, stop=3, step=1)
    
  • 您可以通过columns属性访问的

    df.columns
    
    Index(['xy', 'wz', 'hi', 'kq'], dtype='object')
    

If you want the columns as a list, use the to_listmethod

如果要将列作为列表,请使用该to_list方法

df.columns.tolist()

['xy', 'wz', 'hi', 'kq']