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

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

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?
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
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