Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - How to conver DataFrame to Array?

I have a DataFrame containing only numerical values. Now, what I'd like to do is extract all the values of this DataFrame as an Array. How can I do this? I know that for a single column, if I do df[!,:x1], then the output is an array. But how to do this for all the columns?

like image 823
Davi Barreira Avatar asked Dec 21 '20 12:12

Davi Barreira


People also ask

How do I create a Dataframe in Julia?

The first dataframes-specific thing we need to is to install the DataFrames package. This is a one-time task. The easiest way to do this is to input ] (right square bracket, located above the return/Enter key on your keyboard) into the Julia terminal. This changes the julia> prompt we saw above into one that says:

How do I convert a pandas Dataframe to an array in Python?

Steps to Convert Pandas DataFrame to NumPy Array. Step 1: Create a DataFrame. To start with a simple example, let’s create a DataFrame with 3 columns. The 3 columns will contain only numeric data (i.e. Step 2: Convert the DataFrame to NumPy Array. Step 3 (optional step): Check the Data Type.

What is the DataFrames package used for?

The DataFrames.jl package provides a vast array of procedures that allow you to manipulate tabular data with rows of heterogeneous types. However, you often have your data stored initially in a matrix.

How much does it cost to get DataFrames?

Get 5 months for $5 a month to access the full title and Packt library. The DataFrames.jl package provides a vast array of procedures that allow you to manipulate tabular data with rows of heterogeneous types. However, you often have your data stored initially in a matrix.


3 Answers

The shortest form seems to be:

julia> Matrix(df)
3×2 Array{Float64,2}:
 0.723835  0.307092
 0.02993   0.0147598
 0.141979  0.0271646

In some scenarios you might want need to specify the type such as Matrix{Union{Missing, Float64}}(df)

like image 65
Przemyslaw Szufel Avatar answered Oct 09 '22 03:10

Przemyslaw Szufel


convert(Matrix, df[:,:])

Try this

like image 24
Andy_101 Avatar answered Oct 09 '22 04:10

Andy_101


You can also use the Tables API for this, in particular the Tables.matrix function:

julia> df = DataFrame(x=rand(3), y=rand(3))
3×2 DataFrame
 Row │ x          y        
     │ Float64    Float64  
─────┼─────────────────────
   1 │ 0.33002    0.180934
   2 │ 0.834302   0.470976
   3 │ 0.0916842  0.45172

julia> Tables.matrix(df)
3×2 Array{Float64,2}:
 0.33002    0.180934
 0.834302   0.470976
 0.0916842  0.45172
like image 3
François Févotte Avatar answered Oct 09 '22 03:10

François Févotte