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
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.
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 .
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.
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'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With