Remove empty spaces or NaNs from lists in column of lists in Python/Pandas Dataframe
<p>I have a Pandas dataframe <code>df</code> that looks like this:</p>

<pre><code>   A  B
1  1  [a,b,d,d]
2  6  [,1,4,d,g]
3  a  [w,1,NaN,x,y,2]
</code></pre>

<p>I need to remove the blank in row 2, and the NaN in row 3 to get:</p>

<pre><code>   A  B
1  1  [a,b,d,d]
2  6  [1,4,d,g]
3  a  [w,1,x,y,2]
</code></pre>

<p>I think applying some kind of lambda list comprehension?  </p>

<pre><code>df.B=df.B.apply(lambda x: x if x not in ['',np.NaN])
</code></pre>

<p>but not working...</p>

<p>You need to work on your comprehension skills :)</p>

<pre><code>import numpy as np

df = pd.DataFrame([
        [1, ['a','b','d','d']],
        [6, ['',1,4,'d','g']],
        ['a', ['w',1,np.nan,'x','y',2]]
    ], columns=['A', 'B'])

df.B.apply(lambda l: [x for x in l if x not in ['', np.nan]])
</code></pre>

<p>where <code>l</code> is the current list and <code>x</code> are elements of <code>l</code>.</p>

python pandas