Julia:将具有多个字符串列的 Dataframe 转换为浮点数组

Phy*_*ent 5 dataframe julia

我有一个 DataFrame,其中有许多 String 列,它们应该是 float64。我想一次转换所有列并将数据帧转换为浮点数组。怎么能做到这一点呢?重要的是,还有一些浮动列。

df = DataFrame(a=["1", "2", "3"], b=["1.1", "2.2", "3.3"], c=[0.1, 0.2, 0.3])
# Verbose option
df.a = parse.(Float64, df.a)
df.b = parse.(Float64, df.b)
matrix = Matrix{Float64}(df)
# Is is possible to do this at once especially when there are float columns too?
# Here parse.(Float64, df.c) would throw an error
Run Code Online (Sandbox Code Playgroud)

Nil*_*dat 4

执行此操作的一种方法是循环遍历字符串列:

\n
for c \xe2\x88\x88 names(df, String)\n    df[!, c]= parse.(Float64, df[!, c])\nend\n
Run Code Online (Sandbox Code Playgroud)\n

Matrix{Float64}请注意,如果您已经将所有内容都转换为浮点数,则不需要,只需Matrix(df)要做即可。

\n