Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia DataFrames filtering from multiple columns

I want to filter a DataFrame on multiple values from different columns. I wrote the following code, but it's giving me an error, ERROR: TypeError: non-boolean (BitArray{1}) used in boolean context.

df[((df.eruption .== "CC2011") && (df.dataset .== "MODIS.NDVI.CDI")), :]

Where eruption and dataset are columns in the dataframe

Any ideas what I am doing incorrectly?

like image 827
imantha Avatar asked Dec 18 '22 11:12

imantha


2 Answers

Apart from the answer by Sankios there are the following alternatives:

filter(row -> row.eruption == "CC2011" && row.dataset == "MODIS.NDVI.CDI", df)

and (faster but more verbose):

filter([:eruption, :dataset] => (x, y) -> x == "CC2011" && y == "MODIS.NDVI.CDI", df)

and soon (in DataFrames.jl 1.0 release) you will be able to write:

subset(df, :eruption => ByRow(==("CC2011")), :dataset => ByRow(==("MODIS.NDVI.CDI")))
like image 152
Bogumił Kamiński Avatar answered Dec 24 '22 14:12

Bogumił Kamiński


You can simply concatenate the conditions with the & operator and putting each condition in brackets ().

df[ ( df.eruption .== "CC2011" ) .& ( df.dataset .== "MODIS.NDVI.CDI" ), :]
like image 39
Sankios Avatar answered Dec 24 '22 13:12

Sankios