简体   繁体   English

python-使用numpy loadtxt读取每列具有不同数据类型的csv文件

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

I created a csv file with two columns, the first column is time data, and the second one is some measured data values. 我创建了一个包含两列的csv文件,第一列是时间数据,第二列是一些测量数据值。

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. 现在,我想使用numpy.loadtxt函数将这两列读入两个不同的numpy数组中,日期numpy.loadtxt字符串数据类型,值numpy.loadtxt整数数据类型。

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! 谢谢你的帮助!

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)

I would generally suggest np.genfromtxt if you have something that np.loadtxt can't handle, but they both struggle with space delimited files if there is missing data. 我通常建议np.genfromtxt如果您有np.loadtxt无法处理的内容,但是如果缺少数据,它们都将使用空格分隔的文件。 It'd be hard to define how many missing data points there are without a comma separator for instance. 例如,如果没有逗号分隔符,很难定义缺少的数据点的数量。

A similar function that may work is pd.read_csv or pd.read_table (same thing mostly), which does take care of this issue. 可能起作用的类似功能是pd.read_csvpd.read_table (大多数情况下都是相同的),它确实可以解决此问题。 Just make sure to set the parameter delim_whitespace to True with this file formatting. 只要确保使用此文件格式将参数delim_whitespace设置为True

pd.read_table('TS.csv', delim_whitespace=True, header=None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM