2017-02-12 46 views
2

我认为这是一个微不足道的问题,但我不能让它工作。如何用其他系列替换熊猫数据框的子集

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 

我serires:

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

我想更换一个特定的列,让它成为 '二'。我的系列称为填充,其中列'两'符合条件:是南。残友帮助我吗? 我期望的结果:

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 

回答

5

我认为你需要locisnull通过Series.values代替从fill创建numpy array

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 
相关问题