julia - 如何在 julia 数据框中获取列的 dtypes

标签 julia

如何在 julia.xml 中获取所有列和特定列的 dtypes。具体来说,julia 中 df.dtypes 的 pandas 等价物是什么?

例如, 我有一个像下面这样的df,

│ Row │ Id    │ name   │ item location │
│     │ Int64 │ String │ String        │
├─────┼───────┼────────┼───────────────┤
│ 1   │ 1     │ A      │ xyz           │
│ 2   │ 2     │ B      │ abc           │
│ 3   │ 3     │ C      │ def           │
│ 4   │ 4     │ D      │ ghi           │
│ 5   │ 5     │ E      │ xyz           │
│ 6   │ 6     │ F      │ abc           │
│ 7   │ 7     │ G      │ def           │
│ 8   │ 8     │ H      │ ghi           │
│ 9   │ 9     │ I      │ xyz           │
│ 10  │ 10    │ J      │ abc           │

预期输出:

{'id':  Int64, 'name': String, 'item location': String}

Julia中如何获取dtypes,即Int64 │ String │ String

最佳答案

您已经指定了两个不同的预期输出,所以我在这里展示如何获得两者:

julia> df = DataFrame("Id" => 1, "name" => "A", "item_location" => "xyz")
1×3 DataFrame
│ Row │ Id    │ name   │ item_location │
│     │ Int64 │ String │ String        │
├─────┼───────┼────────┼───────────────┤
│ 1   │ 1     │ A      │ xyz           │

julia> eltype.(eachcol(df))
3-element Array{DataType,1}:
 Int64
 String
 String

julia> Dict(names(df) .=> eltype.(eachcol(df)))
Dict{String,DataType} with 3 entries:
  "Id"            => Int64
  "name"          => String
  "item_location" => String

此外,如果您想将结果存储在 DataFrame 而不是 Dict 中,您可以编写(参见 mapcols 文档 here ):

julia> mapcols(eltype, df)
1×3 DataFrame
│ Row │ Id       │ name     │ item_location │
│     │ DataType │ DataType │ DataType      │
├─────┼──────────┼──────────┼───────────────┤
│ 1   │ Int64    │ String   │ String        │

如果您想要一个 NamedTuple 存储此信息,请写入(Tables.columntable 的文档为 here):

julia> map(eltype, Tables.columntable(df))
(Id = Int64, name = String, item_location = String)

(在这种情况下,请注意,对于非常宽的表,这可能会产生一些额外的编译成本,因为每次调用它时,您都可能获得一种新类型的 NamedTuple)

关于julia - 如何在 julia 数据框中获取列的 dtypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62741210/

相关文章:

julia - 在 Julia 中将十六进制字符串转换为 base64

pointers - 我可以在 julia 中定义指针吗?

for-loop - Julia - for 循环中的上一个和下一个值

julia - 如何重命名 Julia 字典中的键?

julia - 为什么变量可以在 Julia 宏名称修改中以 # 开头?

julia - 在 Julia 中降级到 DifferentialEquations v6.0

metaprogramming - 函数模板的宏 : escaping issues

julia - 如果值为正则打印额外的空格

arrays - 在 Julia 中将整数数组转换为字符串数组

linker - 链接器是否更喜欢 .so 文件而不是 .a 文件?