如何有条件地选择然后更改DataFrame的列?

时间:2019-08-05 19:21:22

标签: julia

我正在寻找R的dplyr中的mutate_if()。例如,如何选择Int64列并将其转换为Float?

using DataFrames
df = DataFrame(A = [72, 38, 54],
               B = [1, 2, 3],
               C = ["red", "blue", "green"])
# convert integer columns to decimal columns without selecting them by name
df

1 个答案:

答案 0 :(得分:2)

是的。该代码将具有任何整数类型(例如Int64Int32)的列转换为Float64

for col in findall(x -> x <:Integer, eltypes(df))
   df[!, col] = Float64.(df2[!, col])
end