Reputation: 137
I am converting java code into a python for re-writing an legacy etl application. In my legacy application the logical expression for data filtration is like below:
// code sample : use input_row to define the condition. // input_row.columnName1.equals("foo") ||!(input_row.columnName2.equals("bar"))
(!(row3.one_GRP_NM == null && row3.two_GRP_NM == null) &&
!(row3.one_GRP_NM != null && row3.one_GRP_NM.equals(row3.two_GRP_NM)))
Now I converted into below -
df[((((df['one_grp_nm']=='null') & (df['two_grp_nm']=='null')) &
((df['one_grp_nm']!='null') & (df['one_grp_nm']==df['two_grp_nm'])))]
But I am not able to figure it out how to convert the ! expression at the beginning of the logic. Could you please guide how to implement not matching/not in in pandas df.
Upvotes: 0
Views: 80
Reputation: 137
thanks lot for your help. Looks like I need to modify little bit for the null column as well ->
df[(~((df['one_grp_nm'].isnull()) & (df['two_grp_nm'].isnull())) &\
~(((df['one_grp_nm'].notna()) & (df['one_grp_nm']==df['two_grp_nm'])))
)]
Upvotes: 1
Reputation: 1420
In Pandas, negation (i.e. not) is denoted using a ~
For your application, it should be :
df[((~((df['one_grp_nm']=='null') & (df['two_grp_nm']=='null')) &\
~((df['one_grp_nm']!='null') & (df['one_grp_nm']==df['two_grp_nm']))))]
Note that if you are breaking across line after &
, you should add a backslash \
otherwise it will be a syntax error.
Another example on not in
in Pandas:
match = [1,2,3]
df = pd.DataFrame({'col_1':[1,2,3,4,5,6]})
# Note the ~
# You can read it as `not (isin)`
df[~df['col_1'].isin(match)]
will give you:
col_1
3 4
4 5
5 6
Upvotes: 1