Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: How to obtain the types of every column of a DataFrame/Table?

There must be an easy way to obtain the column types of every column of a DataFrame. Currently, I do

using DataFrames
a = DataFrame(a = [1,2,3], str = ["a","b","c"], f = [0.0, 1.0, 5.0])
[eltype(Array(col)) for col in eachcol(a)]

Is the best way? Appreciate more efficient and elegant solutions

like image 920
xiaodai Avatar asked Sep 10 '19 02:09

xiaodai


People also ask

How do you find the type of each column in a DataFrame?

To check the data type in pandas DataFrame we can use the “dtype” attribute. The attribute returns a series with the data type of each column. And the column names of the DataFrame are represented as the index of the resultant series object and the corresponding data types are returned as values of the series object.

How do I change the column name in Julia DataFrame?

Additionally, in your example, you should use select! in order to modify the column names in place, or alternatively do 'df = select(df, "col1" => "Id", "col2" => "Name")` as select always return a new DataFrame .

Does Julia have DataFrames?

The Julia DataFrames package is an alternative to Python's Pandas package, but can be used with Pandas using the Pandas. jl wrapper package. Julia-only packages Query.

How do you use DataFrame in Julia?

You can simply create a data frame using the DataFrame() function. You can mention the columns and their values in between the brackets of the DataFrame() function as the argument and run it as shown below. Before this you have to tell Julia that you are going to use data frames by using the command 'using DataFrames'.


1 Answers

You can broadcast eltype over the columns of the data frame:

df = DataFrame(
    x = [1, 2, 3],
    y = ["a", "b", "c"],
    z = [0.0, 1.0, 5.0]
)
julia> eltype.(eachcol(df))
3-element Array{DataType,1}:
 Int64
 String
 Float64

Note: Older versions of DataFrames.jl had an eltypes function, but it has been deprecated and removed from the package.

like image 153
Cameron Bieganek Avatar answered Oct 22 '22 00:10

Cameron Bieganek