2016-05-13 7 views

答えて

2

In [3]: 
df1 = pd.DataFrame(np.random.randn(5,3), columns=list('abc')) 
df2 = pd.DataFrame(np.random.randn(5,3), columns=list('abc')) 
df3 = pd.DataFrame(np.random.randn(5,3), columns=list('abc')) 

print(df1) 
print(df2) 
print(df3) 

      a   b   c 
0 -0.378401 1.456254 -0.327311 
1 0.491864 -0.757420 -0.014689 
2 0.028873 -0.906428 -0.252586 
3 -0.686849 1.515643 1.065322 
4 0.570760 -0.857298 -0.152426 

      a   b   c 
0 1.273215 1.275937 -0.745570 
1 -0.460257 -0.756481 1.043673 
2 0.452731 1.071703 -0.454962 
3 0.418926 1.395290 -1.365873 
4 -0.661421 0.798266 0.384397 

      a   b   c 
0 -0.641351 -1.469222 0.160428 
1 1.164031 1.781090 -1.218099 
2 0.096094 0.821062 0.815384 
3 -1.001950 -1.851345 0.772869 
4 -1.137854 1.205580 -0.922832 

In [4]: 
df = df1.where(df2 >0, df3) 
df 

Out[4]: 
      a   b   c 
0 -0.378401 1.456254 0.160428 
1 1.164031 1.781090 -0.014689 
2 0.028873 -0.906428 0.815384 
3 -0.686849 1.515643 0.772869 
4 -1.137854 -0.857298 -0.152426 
+0

おかげで、それが可能です結果をデータフレームとして保持するには? – wh408

+1

'df.where'で' combine_first'よりもパフォーマンスが向上しました。 – root

+0

はい、df.whereは私のテストでもパフォーマンスが向上します – wh408

1

df = df1[df2 > 0].combine_first(df3) 
関連する問題