Drop Rows By Multiple Column Criteria In Dataframe
I have a pandas dataframe that I'm trying to drop rows based on a criteria across select columns. If the values in these select columns are zero, the rows should be dropped. Here
Solution 1:
Your problem here is that you first assigned the result of your boolean condition: t = t[t[cols_of_interest]!=0]
which overwrites your original df and sets where the condition is not met with NaN
values.
What you want to do is generate the boolean mask, then drop the NaN
rows and pass thresh=1
so that there must be at least a single non-NaN
value in that row, we can then use loc
and use the index of this to get the desired df:
In [124]:
cols_of_interest = ['a','b']
t.loc[t[t[cols_of_interest]!=0].dropna(thresh=1).index]
Out[124]:
a b c
011110223204
EDIT
As pointed out by @DSM you can achieve this simply by using any
and passing axis=1
to test the condition and use this to index into your df:
In [125]:
t[(t[cols_of_interest] != 0).any(axis=1)]
Out[125]:
a b c
011110223204
Post a Comment for "Drop Rows By Multiple Column Criteria In Dataframe"