Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas: select columns with all zero entries in dataframe

Given a dataframe how to find out all the columns that only have 0 as the values?

df    0  1  2  3  4  5  6  7 0  0  0  0  1  0  0  1  0 1  1  1  0  0  0  1  1  1 

Expected output

   2  4 0  0  0 1  0  0 
like image 774
user511792 Avatar asked May 10 '13 16:05

user511792


People also ask

How do you select all columns of a DataFrame except the ones specified?

To select all columns except one column in Pandas DataFrame, we can use df. loc[:, df. columns != <column name>].

How to select columns from Dataframe in Python pandas?

Use Python Pandas and select columns from DataFrames. Follow our tutorial with code examples and learn different ways to select your data today! If you have a DataFrame and would like to access or select a specific few rows/columns from that DataFrame, you can use square brackets or other advanced methods such as loc and iloc.

How to check if a Dataframe column contains zero values only?

We need to create a Dataframe with multiple columns and rows where we will check if a column has zero values only. We will use the all () function to check whether a column contains zero value rows only.

How do I select a specific row/column from a Dataframe?

If you have a DataFrame and would like to access or select a specific few rows/columns from that DataFrame, you can use square brackets or other advanced methods such as loc and iloc. Now suppose that you want to select the country column from the brics DataFrame.

How to index a column as a series in pandas?

This can be done by selecting the column as a series in Pandas. You can pass the column name as a string to the indexing operator. For example, to select only the Name column, you can write: Doing this, this returns the following:


1 Answers

I'd simply compare the values to 0 and use .all():

>>> df = pd.DataFrame(np.random.randint(0, 2, (2, 8))) >>> df    0  1  2  3  4  5  6  7 0  0  0  0  1  0  0  1  0 1  1  1  0  0  0  1  1  1 >>> df == 0        0      1     2      3     4      5      6      7 0   True   True  True  False  True   True  False   True 1  False  False  True   True  True  False  False  False >>> (df == 0).all() 0    False 1    False 2     True 3    False 4     True 5    False 6    False 7    False dtype: bool >>> df.columns[(df == 0).all()] Int64Index([u'2', u'4'], dtype=int64) >>> df.loc[:, (df == 0).all()]    2  4 0  0  0 1  0  0 
like image 65
DSM Avatar answered Sep 21 '22 00:09

DSM