Home > database >  Reading one column of a csv file using numpy
Reading one column of a csv file using numpy

Time:08-25

I have a file test1 which has 2 columns and about 2000 rows. i want to work on just 1st column. I am using numpy for the same but the program is printing elements of both the columns. My data is like this:

59.806   -177.848
   -49.707     82.527
    54.281   -179.839
    93.360   -142.940
   -79.284     48.925
    50.487    173.136
.
.
.
.
.

   -60.811     66.818
    56.226   -178.830
    54.490   -175.354
   -63.786     52.221
   -65.698     52.403
   -45.539     71.237
    34.794    164.026

this is what i have tried

X = pd.read_csv('/home/psdlab/test1.dat')
x_data=df_numpy[:,0]
print x_data

And i am getting the following result

['   -49.707     82.527' '    54.281   -179.839' '    93.360   -142.940'
 ... '   -65.698     52.403' '   -45.539     71.237'
 '    34.794    164.026']

question : how to use numpy to print just 1st column.

CodePudding user response:

first you must define define the np.arange and reshape for the data, for example df_numpy = np.arange(4000).reshape(2000,2), because you have 2 column and 2000 rows. After you define it, you can define x_data=df_numpy[:,0], the last, print (x_data)

CodePudding user response:

Please change your file reading sep in pd.read_csv. Currently, pandas is reading files assuming that each data row has a comma as a column separator. try this: sep='\s '.

  • Related