Python 具有多个值的Numpy genfromtxt读取单元格
我有一个包含多个参数的数据文件,其中一些参数是RGB格式的颜色值,即在每个颜色单元中都有数组(R、G、B) 其余单元格包含一个值,这将导致错误Python 具有多个值的Numpy genfromtxt读取单元格,python,numpy,Python,Numpy,我有一个包含多个参数的数据文件,其中一些参数是RGB格式的颜色值,即在每个颜色单元中都有数组(R、G、B) 其余单元格包含一个值,这将导致错误 Line #11175 (got 38 columns instead of 47) 这是数据文件的示例(每个值用一个选项卡分隔): 我通过调用data=np.genfromtxt(file)读取数据,其中file是上面的数据 我怎样才能解决这个问题?有没有从单元格中读取数组的方法?我认为将数据存储为numpyarray是否存在混合数据类型,不仅在列之
Line #11175 (got 38 columns instead of 47)
这是数据文件的示例(每个值用一个选项卡分隔):
我通过调用data=np.genfromtxt(file)
读取数据,其中file是上面的数据
我怎样才能解决这个问题?有没有从单元格中读取数组的方法?我认为将数据存储为
numpy
array
是否存在混合数据类型,不仅在列之间,而且在某些列中pandas
dataframe
自然更适合这种类型的东西:
除了简单的read\u csv
,所需的唯一步骤就是使用.map(eval)
将读取为字符串的元组转换回元组:
如果你发布了实际的数据和代码,这样其他人就可以重现你的错误,那会有帮助的。好的,我现在就编辑,谢谢。查找有关阅读带空格的引号字符串的问题。例如,尝试使用python
csv
阅读器,将()
作为引号
。或者对行进行预处理,将有问题的字符串转换为genfromtxt
可以作为字符串值处理的内容。您有两个问题-读取每行中相同数量的列,并在最后三个数据字段中存储相同类型的值。您想要什么dtype
呢?data=np.genfromtxt(文件,分隔符='\t',dtype=None)会发生什么情况?如果字段由制表符分隔,并且(0.8,0.8,0.8)
中没有任何制表符,则不应出现列计数问题。
RightValidity Vergence FixationDist EventTimeStamp EventName EventType EventId Code Parameters Value for trial 1 Value for trial 2 Value for trial 3
4 3.5522 0.613 1537.011 InputEvent Mouse_DW 2 999 aperture yes/no 1 1 1
4 3.5522 0.613 1736.592 InputEvent Mouse_UP 2 999 aperture color (0.8, 0.8, 0.8) (0.8, 0.8, 0.8) (0.8, 0.8, 0.8)
4 3.5522 0.613 1752.87 TrialEvent 0 START 8 aperture division 3 3 3
In [20]:
import pandas as pd
df = pd.read_csv('temp.txt', sep='\t')
In [21]:
print df
RightValidity Vergence FixationDist EventTimeStamp EventName EventType \
4 3.5522 0.613 1537.011 InputEvent Mouse_DW 2
4 3.5522 0.613 1736.592 InputEvent Mouse_UP 2
4 3.5522 0.613 1752.870 TrialEvent 0 START
EventId Code Parameters Value for trial 1 Value for trial 2 \
4 999 aperture yes/no 1 1
4 999 aperture color (0.8, 0.8, 0.8) (0.8, 0.8, 0.8)
4 8 aperture division 3 3
Value for trial 3
4 1
4 (0.8, 0.8, 0.8)
4 3
In [22]:
df['Value for trial 1'].values #data not stored in tuples
Out[22]:
array(['1', '(0.8, 0.8, 0.8)', '3'], dtype=object)
In [24]:
df['Value for trial 1'].map(eval).values #so we convert the tuples to tuples, leave numerical numbre unchanged
Out[24]:
array([1, (0.8, 0.8, 0.8), 3], dtype=object)
In [25]:
df['Value for trial 1'] = df['Value for trial 1'].map(eval)
df['Value for trial 2'] = df['Value for trial 2'].map(eval)
df['Value for trial 3'] = df['Value for trial 3'].map(eval)