Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the column names of a python numpy array

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

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 post but the solution provides me with None.

like image 985
ebrahimi Avatar asked Dec 01 '17 07:12

ebrahimi


2 Answers

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:

xy   wz  hi kq
0    10  5  6
1    2   4  7
2    5   2  6
like image 104
Ahmad Avatar answered Oct 05 '22 11:10

Ahmad


Let's assume your csv file looks like

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

Then use pd.read_csv to dump the file into a dataframe

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

  • data which you can access via the values attribute

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

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

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

If you want the columns as a list, use the to_list method

df.columns.tolist()

['xy', 'wz', 'hi', 'kq']
like image 32
piRSquared Avatar answered Oct 05 '22 11:10

piRSquared