user2138149
user2138149

Reputation: 16693

What is the equivalent of DataFrame.dtypes in Julia?

In Python (pandas) a DataFrame has an attribute dtypes which is a dictionary containing a mapping of column names to their respective datatype.

Does Julia's DataFrames library have an equivalent?

I did look in the docs, but didn't find anything...

Upvotes: 1

Views: 55

Answers (2)

BallpointBen
BallpointBen

Reputation: 13750

You can use something like this to get a Dict{String, DataType}:

Dict(zip(names(df), map(eltype, eachcol(df))))

Upvotes: 4

Andre Wildberg
Andre Wildberg

Reputation: 19088

Not a dedicated method but this gets you the desired output relatively straightforward.

If this is your data

using DataFrames

df = DataFrame([["str1", "str2", "str3"], 1:3, 2:4, 3:5], :auto)
3×4 DataFrame
 Row │ x1      x2     x3     x4    
     │ String  Int64  Int64  Int64 
─────┼─────────────────────────────
   1 │ str1        1      2      3
   2 │ str2        2      3      4
   3 │ str3        3      4      5

Function

function dtypes(dframe) 
  hcat(names(dframe), [typeof(i) for i in eachcol(dframe)])
end

getting the desired info

dtypes(df)
4×2 Matrix{Any}:
 "x1"  Vector{String}
 "x2"  Vector{Int64}
 "x3"  Vector{Int64}
 "x4"  Vector{Int64}

Upvotes: 2

Related Questions