Pandas Select DataFrame Columns Using Logical

I want to use a boolean to select columns with more than 4000 entries in a comb that has more than 1000 columns. This expression gives me a logical (True / False) result:

 criteria = comb.ix[:,'c_0327':].count()>4000 

I want to use it to select only True columns for a new Dataframe.
The following just gives me the “Fatal Boolean Series Key”:

 comb.loc[criteria,] 

I also tried:

 comb.ix[:, comb.ix[:,'c_0327':].count()>4000] 

As in this question, the answer is Boolean selection of data in the frame by columns rather than rows, but the same error occurs: "An unchangeable logical key of the series is provided"

 comb.ix[:,'c_0327':].count()>4000 

outputs:

 c_0327 False c_0328 False c_0329 False c_0330 False c_0331 False c_0332 False c_0333 False c_0334 False c_0335 False c_0336 False c_0337 True c_0338 False ..... 
+17
source share
4 answers

A series is returned with column names as index and logical values ​​as row values.

I think you really want:

this should work now:

 comb[criteria.index[criteria]] 

It mainly uses index values ​​from criteria and logical values ​​to mask them, it will return an array of column names, we can use this to select columns of interest from the df source.

+22
source

You can also use:

  # To filter columns (assuming criteria length is equal to the number of columns of comb)
 comb.ix [:, criteria]
 comb.iloc [:, criteria.values]

 # To filter rows (assuming criteria length is equal to the number of rows of comb)
 comb [criteria]
+7
source

I use it, it's cleaner

 comb.values[:,criteria] 

credit: fooobar.com/questions/631135 / ...

+1
source

In pandas 0.25:

 comb.loc[:, criteria] 

Returns a data frame with columns selected by or near a logical list.

For anyone trying to use multiple criteria,

 comb.loc[:, criteria1 & criteria2] 

Note : Using and here instead of & does NOT work. This is due to the fact that and tries to determine the logical value of the entire array, while & works element-wise. This is discussed in Boolean operators for logical indexing in Pandas .

+1
source

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


All Articles