Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the columns of a dataframe that have only positive values in Julia?

I'm working with Julia and trying to edit a DataFrame. My objective is to keep the columns in which at least one value is <=0.

For example, using the code

using DataFrames

data = Dict("A"=>[1,7,2], 
    "B"=>[3,-9,-3], 
    "C"=>[3,0,6], 
    "D"=>[8,4,2], 
    "E"=>[4,3,-4])

df = DataFrame(data)

I get the following result

enter image description here

And I'm trying to transform it into the following DataFrame

enter image description here

There are some options to remove rows based on conditions, but I haven't found any that does the same to columns.

Does anyone know how to do this in a simple way?

like image 323
vzehnder Avatar asked Nov 18 '25 01:11

vzehnder


1 Answers

There are many ways to do this as DataFrames offers the full flexibility of Julia base, but a simple and in my opinion clear way is:

julia> data[!, any.(<=(0), eachcol(data))]
3×2 DataFrame
 Row │ B      E     
     │ Int64  Int64 
─────┼──────────────
   1 │     3      4
   2 │    -9      3
   3 │    -3     -4

Let me also add that you don't have to create a Dict to create a DataFrame, the constructor works fine with String / Vector pairs:

julia> data = DataFrame("A"=>[1,7,2], 
           "B"=>[3,-9,-3], 
           "C"=>[3,0,6], 
           "D"=>[8,4,2], 
           "E"=>[4,3,-4])
3×5 DataFrame
 Row │ A      B      C      D      E     
     │ Int64  Int64  Int64  Int64  Int64 
─────┼───────────────────────────────────
   1 │     1      3      3      8      4
   2 │     7     -9      0      4      3
   3 │     2     -3      6      2     -4
like image 55
Nils Gudat Avatar answered Nov 21 '25 09:11

Nils Gudat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!