Search code examples
pythonpandasdata-analysismissing-data

How to replace subset of pandas dataframe with on other series


I think this is a trivial question, but i just cant make it work.

d = {   'one': pd.Series([1,2,3,4], index=['a', 'b', 'c', 'd']),
            'two': pd.Series([np.nan,6,np.nan,8], index=['a', 'b', 'c', 'd']),
            'three': pd.Series([10,20,30,np.nan], index = ['a', 'b', 'c', 'd'])}         
   ​    
df = pd.DataFrame(d)
df

    one     three   two
a   1       10.0    NaN
b   2       20.0    6.0
c   3       30.0    NaN
d   4       NaN     8.0

My serires:

​fill = pd.Series([30,60])

I'd like to replace a specific column, let it be 'two'. With my Series called fill, where the column 'two' meets a condition: is Nan. Canyou help me with that? My desired result:

df

    one     three   two
a   1       10.0    30
b   2       20.0    6.0
c   3       30.0    60
d   4       NaN     8.0

Solution

  • I think you need loc with isnull for replace numpy array created from fill by Series.values:

    df.loc[df.two.isnull(), 'two'] = fill.values
    print (df)
       one  three   two
    a    1   10.0  30.0
    b    2   20.0   6.0
    c    3   30.0  60.0
    d    4    NaN   8.0