1

This is my csv file:

A  B  C  D  J
0  1  0  0  0
0  0  0  0  0
1  1  1  0  0
0  0  0  0  0
0  0  7  0  7

I need each time to select two columns and I verify this condition if I have Two 0 I delete the row so for exemple I select A and B

Input

A  B  
0  1  
0  0  
1  1  
0  0  
0  0  

Output 
A  B  
0  1  
1  1  

And Then I select A and C ..

I used This code for A and B but it return errors

import pandas as pd 
df = pd.read_csv('Book1.csv')

a=df['A']
b=df['B']

indexes_to_drop = []

for i in df.index:
   if df[(a==0) & (b==0)] :
   indexes_to_drop.append(i)

df.drop(df.index[indexes_to_drop], inplace=True )

Any help please!

3
  • is the check just for specific 2 columns? Commented Jul 4, 2019 at 15:57
  • yes each time for just two columns Commented Jul 4, 2019 at 15:59
  • so you need to check every combinations of columns? can you explain the problem Commented Jul 4, 2019 at 16:04

2 Answers 2

1

First we make your desired combinations of column A with all the rest, then we use iloc to select the correct rows per column combination:

idx_ranges = [[0,i] for i in range(1, len(df.columns))]
dfs = [df[df.iloc[:, idx].ne(0).any(axis=1)].iloc[:, idx] for idx in idx_ranges]
print(dfs[0], '\n')
print(dfs[1], '\n')
print(dfs[2], '\n')
print(dfs[3])

   A  B
0  0  1
2  1  1 

   A  C
2  1  1
4  0  7 

   A  D
2  1  0 

   A  J
2  1  0
4  0  7
Sign up to request clarification or add additional context in comments.

Comments

1

Do not iterate. Create a Boolean Series to slice your DataFrame:

cols = ['A', 'B']

m = df[cols].ne(0).any(1)
df.loc[m]

   A  B  C  D  J
0  0  1  0  0  0
2  1  1  1  0  0

You can get all combinations and store them in a dict with itertools.combinations. Use .loc to select both the rows and columns you care about.

from itertools import combinations

d = {c: df.loc[df[list(c)].ne(0).any(1), list(c)]
     for c in list(combinations(df.columns, 2))}

d[('A', 'B')]
#   A  B
#0  0  1
#2  1  1

d[('C', 'J')]
#   C  J
#2  1  0
#4  7  7

1 Comment

@OumaymaHamdi I see, I've added a solution for all two-pair combinations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.