简体   繁体   中英

Update certain values of a column from another dataframe based on condition of multiple columns

My dataframe1:

id    filler       ent    seg    val     text
1     M,0-10       CP     BEC    20       abc
2     M,10-20      D      BWC    30       abc
3     Y,0-10       CP     CCD    40       abc
4     Y,10-20      D      CFC    50       abc

dataframe2:

id    filler       ent    seg    val     text
1     M,0-10       CP     BEC    20       xyz
2     Y,10-20      D      CFC    50       xyz

need to create a result dataframe:

id    filler       ent    seg    val     text
1     M,0-10       CP     BEC    20       xyz
2     M,10-20      D      BWC    30       abc
3     Y,0-10       CP     CCD    40       abc
4     Y,10-20      D      CFC    50       xyz

where its kind of checks whether all the columns apart from text have same value then updates dataframe1 by dataframe 2 my dataframe1 has 100 rows and dataframe2 has 20 rows.

You can perform a left merge of dataframe2 onto dataframe1, and use the indicator column to find values that need updated in dataframe1.

columns = ['id','filler','ent','seg','val','text']

df1 = pd.DataFrame([
    [1, 'M,0-10','CP','BEC',20, 'abc'],
    [2,'M,10-20','D','BWC',30,'abc'],
    [3,'Y,0-10','CP','CCD',40,'abc'],
    [4,'Y,10-20','D','CFC',50,'abc'],
], columns=columns)

df2 = pd.DataFrame([
    [1,'M,0-10','CP','BEC',20,'xyz'],
    [4,'Y,10-20','D','CFC',50,'xyz'],
], columns=columns)

Merge dataframe2 on dataframe1, with indicator column

columns_merge = [x for x in columns if x!='text']
updated = df1.merge(df2, on=columns_merge, how='left', indicator=True)

Compare and set contents that need updated based on indicator column.

same = updated['_merge']=='both'
updated.loc[same,'text_x'] = updated.loc[same,'text_y']

Drop & rename columns

updated.drop(columns=['text_y','_merge'], inplace=True)
updated.rename(columns = {'text_x': 'text'}, inplace=True)

updated =

   id   filler ent  seg  val text
0   1   M,0-10  CP  BEC   20  xyz
1   2  M,10-20   D  BWC   30  abc
2   3   Y,0-10  CP  CCD   40  abc
3   4  Y,10-20   D  CFC   50  xyz

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