Estcc
Estcc

Reputation: 128

Remove nans from lists in all columsn of a pandas dataframe (pythonic way)

I have a dataframe full of lists (in many columns) and these have nans in them.

I need to eliminate the nans from each column leaving a correct list.

Example of a cell:

['tag_001', 'tag_07', nan, nan, nan] 

How can I remove these nans in a pythonic way?

Thank you!

Upvotes: 2

Views: 215

Answers (1)

William Bright
William Bright

Reputation: 535

the math lib has a function which checks for nan. Easily use the filter function to return a new list that does not have a nan inside of it.

import math

filter(lambda n: not math.isnan(n), ['tag_001', 'tag_07', nan, nan, nan])

Upvotes: 1

Related Questions