Pandas: Filter By Values Within Multiple Columns
I'm trying to filter a dataframe based on the values within the multiple columns, based on a single condition, but keep other columns to which I don't want to apply the filter at a
Solution 1:
You first want to substitute your 'NONE'
with np.nan
so that it is recognized as a null value by dropna
. Then use loc
with your boolean series and column subset. Then use dropna
with axis=1
and how='all'
df.replace('NONE', np.nan) \
.loc[df.month == df.month.max(), l].dropna(axis=1, how='all')
month a
3 2 A
4 2 NONE
Post a Comment for "Pandas: Filter By Values Within Multiple Columns"