Filling missing data by random choosing from non missing values in pandas dataframe
<p>I have a pandas data frame where there are a several missing values. I noticed that the non missing values are close to each other. Thus, I would like to impute the missing values by randomly choosing the non missing values.</p>

<p>For instance:</p>

<pre><code>import pandas as pd
import random
import numpy as np

foo = pd.DataFrame({'A': [2, 3, np.nan, 5, np.nan], 'B':[np.nan, 4, 2, np.nan, 5]})
foo
    A   B
0   2 NaN
1   3   4
2 NaN   2   
3   5 NaN
4 NaN   5
</code></pre>

<p>I would like for instance <code>foo['A'][2]=2</code> and <code>foo['A'][5]=3</code>
The  shape of my pandas DataFrame is (6940,154).
I try this</p>

<pre><code>foo['A'] = foo['A'].fillna(random.choice(foo['A'].values.tolist()))
</code></pre>

<p>But it not working. Could you help me achieve that? Best regards.</p>