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

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

问题描述

我正在尝试将数据框形式的列浮动到字符串.我试过了

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.

谢谢

推荐答案

在您第一次尝试时,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 转换为普通数组 - 如果 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 中将列数据类型从浮点数更改为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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