Pandas DataFrame, How do I remove all columns and rows that sum to 0

Brig

I have a dataFrame with rows and columns that sum to 0.

    A   B   C    D
0   1   1   0    1
1   0   0   0    0 
2   1   0   0    1
3   0   1   0    0  
4   1   1   0    1 

The end result should be

    A   B    D
0   1   1    1
2   1   0    1
3   0   1    0  
4   1   1    1 

Notice the rows and columns that only had zeros have been removed.

unutbu

df.loc[row_indexer, column_indexer] allows you to select rows and columns using boolean masks:

In [88]: df.loc[(df.sum(axis=1) != 0), (df.sum(axis=0) != 0)]
Out[88]: 
   A  B  D
0  1  1  1
2  1  0  1
3  0  1  0
4  1  1  1

[4 rows x 3 columns]

df.sum(axis=1) != 0 is True if and only if the row does not sum to 0.

df.sum(axis=0) != 0 is True if and only if the column does not sum to 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pandas: sum DataFrame rows for given columns

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

pandas: How do I select rows based on the sum of all columns?

From Dev

How do I drop rows from a Pandas dataframe based on data in multiple columns?

From Dev

how to switch columns rows in a pandas dataframe

From Dev

Pandas: How do I split multiple lists in columns into multiple rows?

From Dev

How do I take rows in Pandas Dataframe and transform into values for a Column?

From Dev

How do I remove/omit the count column from the dataframe in Pandas?

From Dev

Not calculating sum for all columns in pandas dataframe

From Dev

Pandas: sum all rows

From Dev

How do I remove a particular level occurring in all factors in a dataframe

From Dev

pandas select rows by condition for all of dataframe columns

From Dev

how do I remove rows with duplicate values of columns in pandas data frame?

From Dev

How do I split a string into several columns in a dataframe with pandas Python?

From Dev

Add Sum to all grouped rows in pandas dataframe

From Dev

How do you filter rows in a pandas dataframe conditional on columns existing?

From Dev

How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

From Dev

pandas dataframe how to sum all value of bigger columns per row

From Dev

How do I add one row of a PANDAS dataframe to the rest of the rows?

From Dev

How do I sum columns of certain rows of my dataset?

From Dev

How do I copy rows in a pandas DataFrame and add an id column

From Dev

How to remove rows where all numerical columns contain zero in Pandas Dataframe with mixed type of columns?

From Dev

How do I get all the rows before a specific index in Pandas?

From Dev

Not calculating sum for all columns in pandas dataframe

From Dev

How to filter Pandas rows by another Dataframe columns?

From Dev

How do I iterate through 2 columns of pandas dataframe data?

From Dev

How to transpose dataframe columns into rows in pandas

From Dev

How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

From Dev

pandas dataframe imported CSV with all rows in one column. How do I fix this?

Related Related

  1. 1

    Pandas: sum DataFrame rows for given columns

  2. 2

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  3. 3

    pandas: How do I select rows based on the sum of all columns?

  4. 4

    How do I drop rows from a Pandas dataframe based on data in multiple columns?

  5. 5

    how to switch columns rows in a pandas dataframe

  6. 6

    Pandas: How do I split multiple lists in columns into multiple rows?

  7. 7

    How do I take rows in Pandas Dataframe and transform into values for a Column?

  8. 8

    How do I remove/omit the count column from the dataframe in Pandas?

  9. 9

    Not calculating sum for all columns in pandas dataframe

  10. 10

    Pandas: sum all rows

  11. 11

    How do I remove a particular level occurring in all factors in a dataframe

  12. 12

    pandas select rows by condition for all of dataframe columns

  13. 13

    how do I remove rows with duplicate values of columns in pandas data frame?

  14. 14

    How do I split a string into several columns in a dataframe with pandas Python?

  15. 15

    Add Sum to all grouped rows in pandas dataframe

  16. 16

    How do you filter rows in a pandas dataframe conditional on columns existing?

  17. 17

    How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

  18. 18

    pandas dataframe how to sum all value of bigger columns per row

  19. 19

    How do I add one row of a PANDAS dataframe to the rest of the rows?

  20. 20

    How do I sum columns of certain rows of my dataset?

  21. 21

    How do I copy rows in a pandas DataFrame and add an id column

  22. 22

    How to remove rows where all numerical columns contain zero in Pandas Dataframe with mixed type of columns?

  23. 23

    How do I get all the rows before a specific index in Pandas?

  24. 24

    Not calculating sum for all columns in pandas dataframe

  25. 25

    How to filter Pandas rows by another Dataframe columns?

  26. 26

    How do I iterate through 2 columns of pandas dataframe data?

  27. 27

    How to transpose dataframe columns into rows in pandas

  28. 28

    How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

  29. 29

    pandas dataframe imported CSV with all rows in one column. How do I fix this?

HotTag

Archive