朱莉娅:使用列名将数组转换为DataFrame

时间:2018-08-08 12:10:27

标签: dataframe julia rename

我试图将数组转换为Julia数据帧并添加列名。不幸的是,我发现的解决方案似乎无效。...

using DataFrames

x = rand(4, 3)
df = convert(DataFrame, x)

# This doesn't work:

rename!(df, ["Col_A", "Col_B", "Col_C"])
names(df) = ["Col_A", "Col_B", "Col_C"])

1 个答案:

答案 0 :(得分:1)

您可以这样做:

using DataFrames

x = rand(4, 3)
df = DataFrame(x)
names!(df, [:Col_A, :Col_B, :Col_C])

或者仅仅是这个:

DataFrame(x, [:Col_A, :Col_B, :Col_C])

请注意,列名必须为Symbol。如果将它们作为字符串,则可以像Symbol.(["Col_A", "Col_B", "Col_C"])那样转换它们。