Select a subset of data rows using multiple conditions

I would like to select a subset of a data frame that satisfies several conditions on several lines. I know that I can do this sequentially: first select the subset that corresponds to the first condition, then part of those that correspond to the second condition, etc., but it looks like it should be completed in one step. It seems like this should work, but no. Apparently, it works the same as in implementations of other DataFrame languages. Any thoughts?

using DataFrames df = DataFrame() df[:A]=[ 1, 3, 4, 7, 9] df[:B]=[ "a", "c", "c", "D", "c"] df[(df[:A].<5)&&(df[:B].=="c"),:] type: non-boolean (DataArray{Bool,1}) used in boolean context while loading In[18], in expression starting on line 5 
+6
source share
2 answers

This is Julia's thing, not the DataFrame thing: you want & instead of && . For instance:

 julia> [true, true] && [false, true] ERROR: TypeError: non-boolean (Array{Bool,1}) used in boolean context julia> [true, true] & [false, true] 2-element Array{Bool,1}: false true julia> df[(df[:A].<5)&(df[:B].=="c"),:] 2x2 DataFrames.DataFrame | Row | A | B | |-----|---|-----| | 1 | 3 | "c" | | 2 | 4 | "c" | 

FWIW, this works similarly in pandas in Python:

 >>> df[(df.A < 5) & (df.B == "c")] AB 1 3 c 2 4 c 
+9
source

I now have the same thing as https://stackoverflow.com/users/5526072/jwimberley that occurs when I upgrade to julia 0.6 from 0.5, and now I use dataframes v 0.10.1.

Update: I fixed the following changes:

 r[(r[:l] .== l) & (r[:w] .== w), :] # julia 0.5 r[.&(r[:l] .== l, r[:w] .== w), :] # julia 0.6 

but this happens very slowly with long chains (time spent on 2 ^ chains) so maybe Query is the best way:

 # r is a dataframe using Query q1 = @from i in r begin @where il == l && iw == w && i.nl == nl && i.lt == lt && i.vz == vz && i.vw == vw && i.vδ == vδ && i.ζx == ζx && i.ζy == ζy && i.ζδx == ζδx @select {absu=i.absu, i.dBU} @collect DataFrame end 

eg. It is fast. It is in the DataFrames documentation.

0
source

Source: https://habr.com/ru/post/984636/


All Articles