如何在Julia中将列数据类型从float更改为string? [英] How can I change column data type from float to string in Julia?

查看:180
本文介绍了如何在Julia中将列数据类型从float更改为string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以浮点数形式将数据框形式的列转换为字符串.我尝试过

I am trying to get a column in a dataframe form float to string. I have tried

df = readtable("data.csv", coltypes = {String, String, String, String, String, Float64, Float64, String});

但是我抱怨了

syntax: { } vector syntax is discontinued

我也尝试过

dfB[:serial] = string(dfB[:serial])

但是它也不起作用.因此,我想知道在Julia中更改列数据类型的正确方法是什么.

but it didn't work either. So, I'd like to know what would be the proper approach to change column data type in Julia.

thx

推荐答案

在您的第一次尝试中,Julia告诉您问题出在哪里-您无法使用{}创建矢量,需要使用[].另外,关键字参数的名称应为eltypes而不是coltypes.

On your first attempt, Julia tells you what the problem is - you can't make a vector with {}, you need to use []. Also, the name of the keyword argument should be eltypes rather than coltypes.

第二次尝试时,您没有浮子,而有一个浮子向量.因此,要更改类型,您需要更改所有元素的类型.在Julia中,矢量的元素运算通过点"语法进行概括,例如string.(collect(dfB[:serial])).当前需要collect才能将DataArray首先转换为普通Array –如果DataArray包含NA,则此操作将失败.恕我直言,DataFrames接口仍然相当不稳定,因此请耐心等待像这样的ATM.

On the second try, you don't have a float, you have a Vector of floats. So to change the type you need to change the type of all elements. In Julia, elementwise operations on vectors are generalized by the 'dot' syntax, e.g. string.(collect(dfB[:serial])) . The collect is needed currently to cast the DataArray to a normal Array first – this will fail if the DataArray contains NAs. IMHO the DataFrames interface is still rather wonky, so expect a few headaches like this ATM.

这篇关于如何在Julia中将列数据类型从float更改为string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆