2017-02-12 9 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]) 

私はそれが '2' であるとする、特定の列を交換したいと思います。私のシリーズではfillと呼ばれていましたが、ここでは 'two'という列は条件を満たす:Nanです。 Canyouは私にそれを手伝ってくれますか? 私の希望する結果:

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

は私がSeries.valuesによってfillから作成numpy arrayを交換するためにあなたがisnulllocが必要だと思う:

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 
関連する問題