Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - using numpy loadtxt reading a csv file with different data types for each column

Tags:

python

csv

numpy

I created a csv file with two columns, the first column is time data, and the second one is some measured data values.

2015/1/1 0:00   5       
2015/1/1 0:15   10    
2015/1/1 0:30   10   
2015/1/1 0:45   15   
2015/1/1 1:00   5  
2015/1/1 1:15   20  
2015/1/1 1:30   20  
2015/1/1 1:45   40  
2015/1/1 2:00   30  
2015/1/1 2:15   20  
2015/1/1 2:30   25  
2015/1/1 2:45   10  
2015/1/1 3:00   
2015/1/1 3:15   
2015/1/1 3:30   
2015/1/1 3:45   
2015/1/1 4:00   
2015/1/1 4:15   
2015/1/1 4:30   30  
2015/1/1 4:45   50  
2015/1/1 5:00   70  

Now I want to use numpy.loadtxt function to read this two columns into two different numpy arrays with string data type for the date column and integer data type for the value column.

I tried different statements to do that, but none of them works.

time, data = np.loadtxt('TS.csv',dtype=str,delimiter=',',usecols=(0, 1),unpack=True)
time, data = np.loadtxt('TS.csv',dtype=(str,int),delimiter=',',usecols=(0, 1),unpack=True)
time, data = np.loadtxt('TS.csv',dtype=[str,int],delimiter=',',usecols=(0, 1),unpack=True)

Does anyone know how to realize the goal I just described? Thanks for your help!

like image 274
Superstar Avatar asked Jul 02 '26 19:07

Superstar


1 Answers

You are very close to what you are looking for. Try this

data = np.loadtxt('TS.csv', dtype='str,int', delimiter=',', usecols=(0, 1), unpack=True)
like image 100
Sitz Blogz Avatar answered Jul 04 '26 09:07

Sitz Blogz