简体   繁体   中英

Pandas join on tuple and frozenset field

I want to a do a join on common field Products. But this common field is represented as different schema in both dataframes as depicted below:

df1 [frozensets]

Products    Products1   
0   (A, C)  (B) 
1   (A, B)  (C) 
2   (C, B)  (A) 
3   (A)     (C, B)  
4   (C)     (A, B)  

df2 [tuples]

Region  Products

France  ('A','B')
France  ('C')
France  ('B','D')
France  ('C','A','B')
France  ('A','B')

In df2, Products are represented as tuples but not in df1. Can someone help me to convert df1['Products'] to tuple so that I can do this join

s1 = pd.merge(df1, df2, how='left', left_on='Products',right_on='Products')

so as the elements are not in string (which they should've been by logic), we can typecast them and make a new tuple of df1 products.

you can try something like this

df_pros = df1['Products'] # or some another way to select all the products from df_1
df_mod = tuple(map(lambda x: (str(x[0]), str(x[1])) if len(x) == 2 else (str(x[0])), list(map(list, df_pros))))
s1 = pd.merge(df_mod, df2, how='left', left_on='Products',right_on='Products')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM