Trying to get columns that has zero for some columns but not zero for others.
import pandas as pd
import numpy as np
df=pd.DataFrame({'t':[0,0,0,np.nan],'t2':[0,0,2,3],'t3':[1,0,0,np.nan],'t4':[0,np.nan,2,3]})
zero=['t','t3']
nozero=['t2','t4']
Dataframe:
t t2 t3 t4
0 0.0 0 1.0 0.0
1 0.0 0 0.0 NaN
2 0.0 2 0.0 2.0
3 NaN 3 NaN 3.0
I have tried:
df[((df[zero]==0).any(axis=1))&((df[nozero]!=0).any(axis=1))]
which gives
t t2 t3 t4
1 0.0 0 0.0 NaN
2 0.0 2 0.0 2.0
also tried:
df[((df[zero]==0)&(df[nozero]!=0)).any(axis=1)]
which gives an empty dataframe.
Expected:
t t2 t3 t4
2 0.0 2 0.0 2.0
Any help would be great. Thanks.
EDIT FOR CLARIFICATION:
I need both conditions(ALL) to be true (zero==0 and nozero!=0) for each pair (t,t2 and t3,t4) but if the row has ANY of these pair true, I want that row.
For example from the following dataframe:
df = pd.DataFrame({'t': [0, 0, 11,0], 't2': [0, 0, 0, 0], 'z3': [0, 0, 12, 0], 't4': [0, 0, 2, 0],
't5': [0, 0, 0, 0], 'z6': [0, 0, 4, 0], 't7': [1, 1, 0, 1], 't8': [1, 1, 0, 1],
'z9': [1, 1, 0, 1], 't10': [1, 1, 0, 1], 't11': [1, 1, 0, 1], 'z12': [1, 1, 0, 1]})
nozero=['z3','z6','z9','z12']
zero=list(set(df.columns)-set(nozero))
t t2 z3 t4 t5 z6 t7 t8 z9 t10 t11 z12
0 0 0 0 0 0 0 1 1 1 1 1 1
1 0 0 0 0 0 0 1 1 1 1 1 1
2 11 0 12 2 0 4 0 0 0 0 0 0
3 0 0 0 0 0 0 1 1 1 1 1 1
I only want row 2 because any(t,t2) is zero and z3 is not zero.
EDIT FOR CLARIFICATION:
The following code generates the rows I want to keep (index=2). Is there a more efficient way than this using .any or .all? (as this requires appending the rows, allocating memory, looping, etc)
import pandas as pd
df = pd.DataFrame({'t': [0, 0, 11,0], 't2': [0, 0, 0, 0], 'z3': [0, 0, 12, 0], 't4': [0, 0, 2, 0],
't5': [0, 0, 0, 0], 'z6': [0, 0, 4, 0], 't7': [1, 1, 0, 1], 't8': [1, 1, 0, 1],
'z9': [1, 1, 0, 1], 't10': [1, 1, 0, 1], 't11': [1, 1, 0, 1], 'z12': [1, 1, 0, 1]})
nozero=['z3','z6','z9','z12']
zero1=['t','t4','t7','t10']
zero2=['t2','t5','t8','t11']
for x,y,z in zip(zero1, zero2, nozero):
print(df[((df[x]==0) | (df[y]==0)) & (df[z]!=0)])