In this post, we will see how to resolve conditional filtering on rows rather than columns
Question:
Given a tablecol_0 | col_1 | col_2 | |
---|---|---|---|
0 | a_00 | a_01 | a_02 |
1 | a_10 | nan | a_12 |
2 | a_20 | a_21 | a_22 |
If I am returning all rows such col_1 does not contain
nan
, then it can be easily done by df[df['col_1'].notnull()]
, which returnscol_0 | col_1 | col_2 | |
---|---|---|---|
0 | a_00 | a_01 | a_02 |
2 | a_20 | a_21 | a_22 |
If I would like to return all columns such that its 1-th row does not contain nan, what should I do? The following is the result that I want:
col_0 | col_2 | |
---|---|---|
0 | a_00 | a_02 |
1 | a_10 | a_12 |
2 | a_20 | a_22 |
I can transpose dataframe, remove rows on transposed dataframe, and transpose back, but it would become inefficient if dataframe is huge. I also tried
Best Answer:
Boolean indexing withloc
along columns axisIf you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
Leave a Review