user10939484
user10939484

Reputation: 177

How to use crosstab for multiple columns?

I need help using crosstab on the df below.

    a       b       c
-------------------------
|   a   |  None |   c   |
|   a   |   b   |  None |
|  None |   b   |   c   |
|   a   |  None |  None |
|  None |  None |  None |

I want to pull rows where more than letter is specified (a&b, a&c, b&c) i.e. rows 1-3. I believe the easiest way to do this is through crosstab (I know I'll get a count but can I also view the rows through this method?). I want to avoid having to write a lengthy 'or' statement to acheive this.

Desired Output:

    a       b       c
-------------------------
|   a   |  None |   c   |
|   a   |   b   |  None |
|  None |   b   |   c   |

Upvotes: 1

Views: 356

Answers (1)

user3483203
user3483203

Reputation: 51155

You aren't looking for crosstab, just check the number of non-nulls using notnull:

df[df.notnull().sum(1).gt(1)]

     a    b    c
0    a  NaN    c
1    a    b  NaN
2  NaN    b    c

Or you can use dropna:

t = 2
df.dropna(thresh=df.shape[1] - t + 1)

     a    b    c
0    a  NaN    c
1    a    b  NaN
2  NaN    b    c

Upvotes: 1

Related Questions