0

How can I delete rows and columns in a Pandas dataframe that contain all the zeros. For example, I have a df:

1 0 1 0 1
0 0 0 0 0
1 1 1 0 1
0 1 1 0 1
1 1 0 0 0
0 0 0 0 0
0 0 1 0 1  

I want to delete 2nd and 6th row (line) and also the 4th column. The output should look like:

1 0 1 1
1 1 1 1
0 1 1 1
1 1 0 0
0 0 1 1  
Mac D
  • 25
  • 5
  • thanks @quang for editing – Mac D Feb 24 '20 at 20:30
  • Are all entries non-negative? If not, you ask two different questions in the title and the body of the post. – hilberts_drinking_problem Feb 24 '20 at 20:38
  • What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: [tour], [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. As an aside, are you using 0/1 instead of actual boolean values? – AMC Feb 25 '20 at 01:21
  • @AMC valid point. I will try to share what i tried and didnt work – Mac D Feb 26 '20 at 16:12

3 Answers3

4

here we go:

ad = np.array([[1, 0, 1, 0, 1],
[0, 0, 0, 0, 0],
[1, 1, 1, 0, 1],
[0, 1, 1, 0, 1],
[1, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 1]])

df = pd.DataFrame(ad)

df.drop(df.loc[df.sum(axis=1)==0].index, inplace=True)
df.drop(columns=df.columns[df.sum()==0], inplace=True)

The code above will drop the row/column, when the sum of the row/column is zero. This is achived by calucating the sum along the axis 1 for rows and 0 for columns and then dropting the row/column with a sum of 0 (df.drop(...))

coco18
  • 836
  • 8
  • 18
0

found similar question already answered

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

df.loc[(df!=0).any(1), (df!=0).any(0)]

But this does not work inplace. How can i change that?

Mac D
  • 25
  • 5
0
df = df.loc[(df!=0).any(1), (df!=0).any(0)]

In addition to the question when someone wants to drop a column/row, where every value is 0, the solution above will do this. First you getting a table with False/True's for the dataframe for the condition df != 0 and with (df != 0).any()) you will get whether any element is True, potentially over an axis (from the documentation).

coco18
  • 836
  • 8
  • 18