Carlos Arronte Bello Carlos Arronte Bello - 2 months ago 10
Python Question

Remove several rows with zero values in a dataframe using python

HI everybody i need some help with python.

I'm working with an excel with several rows, some of this rows has zero value in all the columns, so i need to delete that rows.

In
id a b c d
a 0 1 5 0
b 0 0 0 0
c 0 0 0 0
d 0 0 0 1
e 1 0 0 1

Out
id a b c d
a 0 1 5 0
d 0 0 0 1
e 1 0 0 1


I think in something like show the rows that do not contain zeros, but do not work because is deleting all the rows with zero and without zero

path = '/Users/arronteb/Desktop/excel/ejemplo1.xlsx'
xlsx = pd.ExcelFile(path)
df = pd.read_excel(xlsx,'Sheet1')
df_zero = df[(df.OTC != 0) & (df.TM != 0) & (df.Lease != 0) & (df.Maint != 0) & (df.Support != 0) & (df.Other != 0)]


Then i think like just show the columns with zero

In
id a b c d
a 0 1 5 0
b 0 0 0 0
c 0 0 0 0
d 0 0 0 1
e 1 0 0 1


Out
id a b c d
b 0 0 0 0
c 0 0 0 0


So i make a little change and i have something like this

path = '/Users/arronteb/Desktop/excel/ejemplo1.xlsx'
xlsx = pd.ExcelFile(path)
df = pd.read_excel(xlsx,'Sheet1')
df_zero = df[(df.OTC == 0) & (df.TM == 0) & (df.Lease == 0) & (df.Maint == 0) & (df.Support == 0) & (df.Other == 0)]


In this way I just get the column with zeros. I need a way to remove this 2 rows from the original input, and receive the output without that rows. Thanks, and sorry for the bad English, I'm working on that too

Answer

Given your input you can group by whether all the columns are zero or not, then access them, eg:

groups = df.groupby((df.drop('id', axis= 1) == 0).all(axis=1))
all_zero = groups.get_group(True)
non_all_zero = groups.get_group(False)
Comments