【问题标题】:Compare columns in pandas and merge比较熊猫中的列并合并
【发布时间】:2018-05-10 23:38:13
【问题描述】:

所以我有两个数据框,我想将它们合并到一个名为 offer_codes 的列上。所有行在一个列表中都有多个报价代码(我可能会将其转换为元组),我想将报价代码与第二个数据框匹配并合并它们。其中一个数据框返回一个列表,另一个只是一个值,但我想合并它以便合并。数据框来自网站的销售数据。

df = pd.DataFrame(data={'available': [False, True, True],
                        'count': [190,285,165],
                        'offer_codes': ['no_offer_code',['G545', 'G1891'],['G92182', 'G1921']]})
df2 = pd.DataFrame(data={'price':[85.00,99.00],
                         'offer_codes':['G1891', 'G1921'],
                         'after_fees':[105, 121]})

我想合并这些,但我的问题是当我尝试与元组合并时列表是不可散列的,似乎没有正确匹配。

#first df
   available  count      offer_codes
0      False    190    no_offer_code
1       True    285    [G545, G1891]
2       True    165  [G92182, G1921]
#2nd df
   after_fees offer_codes  price
0         105       G1891   85.0
1         121       G1921   99.0
#after the merge
   after_fees  available  count offer_codes  price
0         105       True    285       G1891   85.0
1         121       True    165       G1921   99.0

我认为将列表放入元组中会起作用,但它肯定没有。

【问题讨论】:

    标签: python python-3.x pandas merge


    【解决方案1】:

    有点长..

    df.set_index(['available','count']).offer_codes.apply(pd.Series).stack().\
          to_frame('offer_codes').\
              reset_index(level['count','available']).\
                merge(df2,on='offer_codes',how='left').dropna()
    Out[59]: 
       available  count offer_codes  after_fees  price
    2       True    285       G1891       105.0   85.0
    4       True    165       G1921       121.0   99.0
    

    【讨论】:

    • 最后一段你可以省略how='left',这样你就不用dropna()了,对吧?
    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 2022-11-27
    • 2017-04-17
    • 2021-01-08
    • 2015-11-30
    • 2019-11-25
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多