简体   繁体   中英

Compare two columns of data using pandas, if same values in rows, update

I have two dataframes read by pd.read_csv using pandas.

data 1:

  animal   name
0 lion     a
1 tiger    b,c
2 tiger    b,c
3 bear     d,e
4 bear     d,e

data 2:

0 lion     w
1 tiger    x,y
2 bear     z

I would like to compare animal column (data 1) against 1st column (data2) and plug-in 2nd column (data2) after the first value of the name column delimited by comma (data 1).

This is the result I want.

  animal   name
0 lion     a,w
1 tiger    b,x,y,c
2 tiger    b,x,y,c
3 bear     d,z,e
4 bear     d,z,e

How to I do this?


Thanks to the inputs below, I have created the code like this and works great. In fact, I wanted to plug in the df2 name column after the first string.

  import sys
  import pandas as pd
  df1 = pd.read_csv("df1.dat", delimiter = '\t')
  df2 = pd.read_csv("df2.dat", delimiter = '\t')
  df1 = df1.fillna('')
  df2 = df2.fillna('')
  df1['first'] = df1['name'].str.split(',').str[0]
  df1['rest'] = df1['name'].str.split(',').str[1:].apply(','.join)
  df1['name'] =df1.merge(df2, how='inner', on='animal')[['first', 'name_y', 'rest']].apply(','.join, 1)
  df1.apply(lambda x: x.str.strip() if x.dtype == "object" else x)
  df2 = df1['name'].str.replace(',,',',')
  df2 = df2.str.rstrip(',')

This cleans out data in case of NaN and removes the not needed commas. For example,

df1:

  animal    name
  lion  a
  tiger b,c
  tiger b,c
  bear  d,f,g
  bear  d,f,g

df2:

   animal   name
   lion 
   tiger    x,y
   bear 

Results in:

   0          a
   1    b,x,y,c
   2    b,x,y,c
   3      d,f,g
   4      d,f,g

Thanks for the feedback!

This should do it:

data_merged = df1.merge(df2, how='inner', on='animal')
data_merged['name'] = data_merged.name_x+','+data_merged.name_y
data_merged.drop(['name_x','name_y'], axis=1, inplace=True)
data_merged


    animal  name
0   lion    a,w
1   tiger   b,c,x,y
2   tiger   b,c,x,y
3   bear    d,e,z
4   bear    d,e,z

Using merge with apply :

df1['name'] = df1.merge(
                    df2, how='inner', on='animal'
              )[['name_x', 'name_y']].apply(','.join, 1)

  animal     name
0   lion      a,w
1  tiger  b,c,x,y
2  tiger  b,c,x,y
3   bear    d,e,z
4   bear    d,e,z

If order is important, you can use a custom helper function:

def helper(x):
    foo, bar = [x[i].split(',') for i in ['name_x', 'name_y']]
    return ','.join(foo[:1] + bar + foo[1:])

df1.merge(df2, how='inner', on='animal')[['name_x', 'name_y']].apply(helper, 1)

0        a,w
1    b,x,y,c
2    b,x,y,c
3      d,z,e
4      d,z,e
dtype: object

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