写入文件时numpy数组更改为字符串 [英] numpy array changes to string when writing to file
问题描述
我有一个数据框,其中的一列是一个numpy数组:
I have a dataframe where one of the columns is a numpy array:
DF
Name Vec
0 Abenakiite-(Ce) [0.0, 0.0, 0.0, 0.0, 0.0, 0.043, 0.0, 0.478, 0...
1 Abernathyite [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...
2 Abhurite [0.176, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.235, 0...
3 Abswurmbachite [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0,...
当我检查每个元素的数据类型时,将返回正确的数据类型.
When I check the data type of each element, the correct data type is returned.
type(DF['Vec'].iloc[1])
numpy.ndarray
我将其保存到一个csv文件中:
I save this into a csv file:
DF.to_csv('.\\file.csv',sep='\t')
现在,当我再次读取文件时,
Now, when I read the file again,
new_DF=pd.read_csv('.\\file.csv',sep='\t')
并检查索引1处的Vec数据类型:
and check the datatype of Vec at index 1:
type(new_DF['Vec'].iloc[1])
str
numpy数组的大小为1x127.
The size of the numpy array is 1x127.
数据类型已从numpy数组更改为字符串.我还可以在各个向量中看到一些新的线元素.我认为这可能是由于将向量写入csv时出现的一些问题,但我不知道如何解决.有人可以帮忙吗?
The data type has changed from a numpy array to a string. I can also see some new line elements in the individual vectors. I think this might be due to some problem when the vector is written into a csv but I don't know how to fix it. Can someone please help?
谢谢!
推荐答案
在注释中,我犯了一个错误,并说dtype
而不是converters
.您想要的是在阅读功能时将其转换.有一些虚拟变量:
In the comments I made a mistake and said dtype
instead of converters
. What you want is to convert them as you read them using a function. With some dummy variables:
df=pd.DataFrame({'name':['name1','name2'],'Vec':[np.array([1,2]),np.array([3,4])]})
df.to_csv('tmp.csv')
def converter(instr):
return np.fromstring(instr[1:-1],sep=' ')
df1=pd.read_csv('tmp.csv',converters={'Vec':converter})
df1.iloc[0,2]
array([1., 2.])
这篇关于写入文件时numpy数组更改为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!