2016-04-07 8 views
1

大きなpandas DataFrameとその値の計算されたDataFrameの別のサブセットがあります。他の値を変更せずに、サブセットDataFrameの値を大きな値にマージしたい。大きなパンダのデータフレームをサブセットの特定の値とマージする

df_large: 
index col_1 col_2 col_3 
1  10 15 33 
2  23 16 nan 
3  33 92 34 
4  132 123 nan 
5  32 59 nan 

サブセット:

df_small: 
index col_1 col_2 col_3 
2  23 16 34 
4  132 123 87 

私は結果のデータフレームは、特定のインデックスがdf_smallに存在するだけdf_small.col_3の値とdf_large.col_3内の値を上書きしたい:

df_large: 
index col_1 col_2 col_3 
1  10 15 33 
2  23 16 34 
3  33 92 34 
4  132 123 87 
5  32 59 nan 

私はマージを見てみましたが、これをどのようにエレガントにするかわかりません。それを行うには(多くの中で)

答えて

2

片道:

df_large.ix[df_small.index, 'col_3'] = df_small.col_3 

速くcombine_first()

In [408]: %timeit df = df_large.combine_first(df_small) 
100 loops, best of 3: 6.45 ms per loop 

In [409]: %timeit df_large.ix[df_small.index, 'col_3'] = df_small.col_3 
100 loops, best of 3: 2.43 ms per loop 
と比較しているようです
関連する問題