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?
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:
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.
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.
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.
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)
convert(Matrix, df[:,:])
Try this
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
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