66659

Select columns in pandas dataframe by value in rows

I have a pandas.DataFrame with too much columns. I want to select all columns with values in rows equals to 0 and 1. Type of all columns is int64 and I can't select they by object or other type. How can I do this?

Answer1:

IIUC then you can use isin and filter the columns:

In [169]: df = pd.DataFrame({'a':[0,1,1,0], 'b':list('abcd'), 'c':[1,2,3,4]}) df Out[169]: a b c 0 0 a 1 1 1 b 2 2 1 c 3 3 0 d 4 In [174]: df[df.columns[df.isin([0,1]).all()]] Out[174]: a 0 0 1 1 2 1 3 0

The output from the inner condition:

In [175]: df.isin([0,1]).all() Out[175]: a True b False c False dtype: bool

We can use the boolean mask to filter the columns:

In [176]: df.columns[df.isin([0,1]).all()] Out[176]: Index(['a'], dtype='object')

Recommend

  • Construct image from 4D list
  • Merging two Dataframes in Pandas based on time-range difference
  • Is ReplaySubject(1) the same as AsyncSubject()?
  • How to rename entries in pandas dataframe?
  • Pandas remap to range in column
  • BigQuery not dealing with timestamp in millisecond with partition column
  • Azure file Storage SMB slow to list files in directory
  • Find and remove matching column values in pyspark
  • finding values in pandas series - Python3
  • Compare Pandas dataframes and add column
  • Multiplying column elements of sparse Matrix
  • iOS 6 dateFromString returns wrong date
  • How to filter on year and quarter in pandas
  • WPF version of .ScaleControl?
  • Primefaces :radioButton inside a ui:repeat
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • R - Combining Columns to String Based on Logical Match
  • Spring security and special characters
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • retrieve vertices with no linked edge in arangodb
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • How do I configure my settings file to work with unit tests?
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • Is it possible to post an object from jquery to bottle.py?
  • reshape alternating columns in less time and using less memory
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases