熊猫合并,“冲突列”中的条件更新

时间:2019-04-02 15:41:34

标签: python-3.x pandas merge

我有两个数据框,df1df2(请参见下文),

  1. merge放在其中一列常用列中

  2. 有条件地更新其他公共列。

样本数据框和预期结果。

df1:
   A    B     C
0  123  1819. NaN  
1  456  NaN   115
2  789  9012. NaN
3  121  8732. NaN
4  883  NaN   171
5  771  8871. 191
# df2:
     C      B
0  115  41853
1  115  22723
2  115  57302
3  115  91494
4  171  43607
5  171  36327
6  191  39874
7  191  25456
8  191  76283
9  191  97506
  1. merge C
  2. 列上
# how='left' is necessary
pd.merge(df1, df2, on='C', how='left')
      A     B_x      C      B_y
0   123  1819.0    NaN      NaN
1   456     NaN  115.0  41853.0
2   456     NaN  115.0  22723.0
3   456     NaN  115.0  57302.0
4   456     NaN  115.0  91494.0
5   789  9012.0    NaN      NaN
6   121  8732.0    NaN      NaN
7   883     NaN  171.0  43607.0
8   883     NaN  171.0  36327.0
9   771     NaN  191.0  39874.0
10  771     NaN  191.0  25456.0
11  771     NaN  191.0  76283.0
12  771     NaN  191.0  97506.0
  1. 有条件地合并列B_xB_y,即用right_table的非NaN值替换left_table( B_x )中的NaN值( B_y

PS:假设 B_x B_y 永远不会同时NaN

最终结果:

      A      C       B
0   123    NaN    1819
1   456  115.0   41853
2   456  115.0   22723
3   456  115.0   57302
4   456  115.0   91494
5   789    NaN    9012
6   121    NaN    8732
7   883  171.0   43607
8   883  171.0   36327
9   771  191.0   39874
10  771  191.0   25456
11  771  191.0   76283
12  771  191.0   97506

我知道函数combine_first,但仅适用于 indices

1 个答案:

答案 0 :(得分:0)

struct ITEMS: Codable { let CODE:String let CAT_ID:String let SUB_ID:String let PRODUCT_AR:String let PRODUCT_EN:String let OLD_PRICE:String let NEW_PRICE:String let UNIT:String let BARCODE:String let THE_DATE:String let TIME:String } struct MyAPIData: Codable { let ITEMS: [ITEMS] } 之后使用let json = try JSONDecoder().decode(MyAPIData.self, from: data)

merge