Conditional merge for CSV files using python (pandas)

萝らか妹 提交于 2019-12-06 08:54:01

One way to smash them together is to use merge (on store_id and number, if these are the index then this would be a join rather than a merge):

In [11]: res = df1.merge(df2, on=['store_id', 'phone'], how='outer')

In [12]: res
Out[12]:
   store_id     address_x        phone           address_y
0      9191  9827 Park st    999999999  9827 Park st Apt82
1      8181  543 Hello st   1111111111                 NaN
2      7171           NaN  87282728282         912 John st

You can then use where to select address_y if it exists, otherwise address_x:

In [13]: res['address'] = res.address_y.where(res.address_y, res.address_x)

In [14]: del res['address_x'], res['address_y']

In [15]: res
Out[15]: 
   store_id        phone             address
0      9191    999999999  9827 Park st Apt82
1      8181   1111111111        543 Hello st
2      7171  87282728282         912 John st

How about use concat, groupby, agg, then you can write a agg function to choose the right value:

import pandas as pd
import io

t1 = """store_id,address,phone
9191,9827 Park st,999999999
8181,543 Hello st,1111111111"""

t2 = """store_id,address,phone
9191,9827 Park st Apt82,999999999
7171,912 John st,87282728282"""

df1 = pd.read_csv(io.BytesIO(t1))
df2 = pd.read_csv(io.BytesIO(t2))

df = pd.concat([df1, df2]).reset_index(drop=True)

def f(s):
    loc = s.str.len().idxmax()
    return s[loc]

df.groupby(["store_id", "phone"]).agg(f)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!