简体   繁体   中英

How to replace pandas dataframe values based on lookup values in another dataframe?

I have a large pandas dataframe with numerical values structured like this:

>>> df1
   A  B  C
0  2  1  2
1  1  2  3
2  2  3  1

I need to replace all of the the above cell values with a 'description' that maps to the field name and cell value as referenced in another dataframe structured like this:

>>> df2
  field_name  code description
0          A     1          NO
1          A     2         YES
2          A     3       MAYBE
3          B     1           x
4          B     2           y
5          B     3           z
6          C     1        GOOD
7          C     2         BAD
8          C     3        FINE

The desired output would be like:

>>> df3
     A  B     C
0  YES  x   BAD
1   NO  y  FINE
2  YES  z  GOOD

I could figure out a way to do this on a small scale using something like.map or.replace - however the actual datasets contain thousands of records with hundreds of different combinations to replace. Any help would be really appreciated.

Thanks.

Use DataFrame.replace with DataFrame.pivot :

df1 = df1.replace(df2.pivot(columns='field_name', index='code', values='description')
                     .to_dict())

maybe you need select columns previously:

df1[cols] = df1[cols].replace(df2.pivot(columns='field_name',
                                        index='code', values='description')
                                 .to_dict())

Output

print(df1)
     A  B     C
0  YES  x   BAD
1   NO  y  FINE
2  YES  z  GOOD

You can unstack df1 , merge with df2 and pivot the result:

df3 = df1.stack().reset_index().rename(
    columns={'level_1': 'field_name', 0: 'code'}).merge(
        df2, 'left', on=['field_name', 'code']).pivot(
            index='level_0', columns='field_name',
            values='description').rename_axis(None).rename_axis(None, axis=1)

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