Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to (better) get NaN data from pandas dataframe into new dataframe?

I have a dataframe and am currently creating a new dataframe with the column names and number of empty cells like this.

empty = pd.DataFrame(columns=['Column', 'NaNs'])
for (columnName, columnData) in dataset.items():
    empty.loc[-1] = [columnName, columnData.isnull().any().sum()]
    empty.index = empty.index + 1
    empty = empty.sort_index()

This is 5 lines for a simple overview table.

I wonder if there’s a better, shorter way of achieving the same with transpose and apply or something else which I could’t figure out so far.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a vectorial approach with isna and sum, then processing the output Series to form a DataFrame:

out = df.isna().sum().rename_axis('Column').reset_index(name='NaNs')

Output, using @Dogbert’s example (df = pd.DataFrame({"a": [0, 1, None], "b": [0, None, 2], "c": [0, None, None]})):

  Column  NaNs
0      a     1
1      b     1
2      c     2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading