J.James
J.James

Reputation: 37

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.

Upvotes: 1

Views: 578

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148900

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)

Upvotes: 0

ansev
ansev

Reputation: 30920

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

Upvotes: 1

Related Questions