Drop rows with all zeros in pandas data frame
It turns out this can be nicely expressed in a vectorized fashion:
> df = pd.DataFrame({'a':[0,0,1,1], 'b':[0,1,0,1]})
> df = df[(df.T != 0).any()]
> df
a b
1 0 1
2 1 0
3 1 1
How to remove rows from a DataFrame where some columns only have zero values
try this,
df[~df[list('cdef')].eq(0).all(axis = 1)]
a b c d e f
0 1 2 3 4 5 6
1 11 22 33 44 55 66
Drop row in pandas dataframe if any value in the row equals zero
i think the easiest way is looking at rows where all values are not equal to 0:
df[(df != 0).all(1)]
How to remove rows with any zero value
There are a few different ways of doing this. I prefer using apply
, since it's easily extendable:
##Generate some data
dd = data.frame(a = 1:4, b= 1:0, c=0:3)
##Go through each row and determine if a value is zero
row_sub = apply(dd, 1, function(row) all(row !=0 ))
##Subset as usual
dd[row_sub,]
remove R Dataframe rows based on zero values in one column
Just subset the data frame based on the value in the No_of_Mails
column:
df[df$No_of_Mails != 0, ]
Demo
Related Topics
Delete Rows That Exist in Another Data Frame
Count Occurrences of Value in a Set of Variables in R (Per Row)
Remove Specific Characters from Column Names in R
Selecting Multiple Odd or Even Columns/Rows for Dataframe
Use First Row Data as Column Names in R
How to Get Rowsums for Selected Columns in R
R Collapse Multiple Rows into 1 Row - Same Columns
How Split Column of List-Values into Multiple Columns
Adding a New Column Based Upon Values in Another Column Using Dplyr
Divide All Columns by the Value from the 2Nd Column - Apply for All Rows
How to Control Ordering of Stacked Bar Chart Using Identity on Ggplot2
Calculate Max Value Across Multiple Columns by Multiple Groups
R: Pulling Data from One Column to Create New Columns
How to Test When Condition Returns Numeric(0) in R
How to Add Row and Column to a Dataframe of Different Length
If Else Statements to Check If a String Contains a Substring in R
Creating a Boxplot for Each Column in R
How to Sort a Data Frame by Alphabetic Order of a Character Variable in R