Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Select DataFrame columns using boolean

Tags:

python

pandas

I want to use a boolean to select the columns with more than 4000 entries from a dataframe comb which has over 1,000 columns. This expression gives me a Boolean (True/False) result:

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

I want to use it to select only the True columns to a new Dataframe.
The following just gives me "Unalignable boolean Series key provided":

comb.loc[criteria,] 

I also tried:

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

Similar to this question answer dataframe boolean selection along columns instead of row but that gives me the same error: "Unalignable boolean Series key provided"

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

yields:

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 ..... 
like image 823
dartdog Avatar asked Mar 26 '15 15:03

dartdog


People also ask

How do you use boolean in Pandas?

Pandas DataFrame bool() MethodThe bool() method returns a boolean value, True or False, reflecting the value of the DataFrame. This method will only work if the DataFrame has only 1 value, and that value must be either True or False, otherwise the bool() method will return an error.

How do I select selective columns in Pandas?

Selecting columns based on their name This is the most basic way to select a single column from a dataframe, just put the string name of the column in brackets. Returns a pandas series. Passing a list in the brackets lets you select multiple columns at the same time.

Can we perform boolean indexing on a DataFrame?

Boolean indexing helps us to select the data from the DataFrames using a boolean vector. We need a DataFrame with a boolean index to use the boolean indexing.

How to filter DataFrames with Boolean masks in pandas?

To filter DataFrames with Boolean Masks we use the index operator and pass a comparison for a specific column. In the example below, pandas will filter all rows for sales greater than 1000.

How to create a Dataframe from a row in pandas?

Rows represents the records/ tuples and columns refers to the attributes. We can create the DataFrame by using pandas.DataFrame () method. We can also create a DataFrame using dictionary by skipping columns and indices.

What is Boolean indexing in pandas?

Boolean Indexing in Pandas. In boolean indexing, we will select subsets of data based on the actual values of the data in the DataFrame and not on their row/column labels or integer locations. In boolean indexing, we use a boolean vector to filter the data.

How do I select a column in a list in pandas?

Using iloc to Select Columns The iloc function is one of the primary way of selecting data in Pandas. The method “iloc” stands for integer location indexing, where rows and columns are selected using their integer positions. This method is great for:


2 Answers

What is returned is a Series with the column names as the index and the boolean values as the row values.

I think actually you want:

this should now work:

comb[criteria.index[criteria]] 

Basically this uses the index values from criteria and the boolean values to mask them, this will return an array of column names, we can use this to select the columns of interest from the orig df.

like image 110
EdChum Avatar answered Oct 04 '22 16:10

EdChum


In pandas 0.25:

comb.loc[:, criteria] 

Returns a DataFrame with columns selected by the Boolean list or Series.

For multiple criteria:

comb.loc[:, criteria1 & criteria2] 

And for selecting rows with an index criteria:

comb[criteria] 

Note: The bit-wise operator & is required (not and). See Logical operators for boolean indexing in Pandas.

Other Note: If the criteria is an expression (e.g., comb.columnX > 3), and multiple criteria are used, remember to enclose each expression in parentheses! This is because &, | have higher precedence than >, ==, ect. (whereas and, or are lower precedence).

like image 31
johnDanger Avatar answered Oct 04 '22 15:10

johnDanger