如何在 julia 中获取所有列和特定列的 dtypes。具体来说df.dtypes,朱莉娅中的熊猫相当于什么?
例如,我有一个如下所示的 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 ?
Run Code Online (Sandbox Code Playgroud)
预期输出:
{'id': Int64, 'name': String, 'item location': String}
Run Code Online (Sandbox Code Playgroud)
如何获得 dtypes,即Int64 ? String ? String 在 Julia 中?
您已经指定了两个不同的预期输出,所以我在这里展示了如何获得这两个输出:
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
Run Code Online (Sandbox Code Playgroud)
此外,如果您想将结果存储在 aDataFrame而不是 aDict您可以编写(请参阅此处的mapcols文档):
julia> mapcols(eltype, df)
1×3 DataFrame
? Row ? Id ? name ? item_location ?
? ? DataType ? DataType ? DataType ?
?????????????????????????????????????????????
? 1 ? Int64 ? String ? String ?
Run Code Online (Sandbox Code Playgroud)
如果你想有一个NamedTuple存储这些信息写入(的文件Tables.columntable是在这里):
julia> map(eltype, Tables.columntable(df))
(Id = Int64, name = String, item_location = String)
Run Code Online (Sandbox Code Playgroud)
(在这种情况下请注意,对于非常宽的表,这可能会产生一些额外的编译成本,因为每次调用它时都可能会获得一种新类型的NamedTuple)