Home > Net >  Pandas: Fast way to get cols/rows containing na
Pandas: Fast way to get cols/rows containing na

Time:04-10

In Pandas we can drop cols/rows by .dropna(how = ..., axis = ...) but is there a way to get an array-like of True/False indicators for each col/row, which would indicate whether a col/row contains na according to how and axis arguments?

I.e. is there a way to convert .dropna(how = ..., axis = ...) to a method, which would instead of actual removal just tell us, which cols/rows would be removed if we called .dropna(...) with specific how and axis.

Thank you for your time!

CodePudding user response:

You can use isna() to replicate the behaviour of dropna without actually removing data. To mimic the 'how' and 'axis' parameter, you can add any() or all() and set the axis accordingly.

Here is a simple example:

import pandas as pd

df = pd.DataFrame([[pd.NA, pd.NA, 1], [pd.NA, pd.NA, pd.NA]])
df.isna()

Output:

    0   1   2
0   True    True    False
1   True    True    True

Eq. to dropna(how='any', axis=0)

df.isna().any(axis=0)

Output:

0    True
1    True
2    True
dtype: bool

Eq. to dropna(how='any', axis=1)

df.isna().any(axis=1)

Output:

0    True
1    True
dtype: bool
  • Related