简体   繁体   English

如何根据另一个 dataframe 中的查找值替换 pandas dataframe 值?

[英]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:我有一个大的 pandas dataframe ,其数值结构如下:

>>> 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:我需要将上述所有单元格值替换为“描述”,该“描述”映射到另一个 dataframe 中引用的字段名称和单元格值,其结构如下:

>>> 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:所需的 output 如下所示:

>>> 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.我可以想出一种方法来使用类似.map 或.replace 之类的小规模执行此操作 - 但是实际数据集包含数千条记录以及数百种不同的组合要替换。 Any help would be really appreciated.任何帮助将非常感激。

Thanks.谢谢。

Use DataFrame.replace with DataFrame.pivot :使用DataFrame.replace . 替换为DataFrame.pivot

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

maybe you need select columns previously:也许您以前需要 select 列:

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

Output 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:您可以取消堆叠df1 ,与df2pivot合并结果:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PANDAS - 如何根据另一个 dataframe 中的查找表替换 dataframe 中的值 - PANDAS - How do I replace the values in a dataframe based on a lookup table in another dataframe 通过从另一个数据帧中查找来替换 Pandas 数据帧中的所有值 - Replace all values in pandas dataframe by lookup from another dataframe 使用另一个 Dataframe 作为查找表替换 Pandas Dataframe 中的值 - Replace values in Pandas Dataframe using another Dataframe as a lookup table 如何用另一个 DataFrame 的值替换 pandas DataFrame? - How to replace pandas DataFrame with the values of another DataFrame? 如何用另一个 dataframe 替换 pandas dataframe 中的值 - How to replace values in pandas dataframe with another dataframe 如何根据另一个数据框中的查找表替换数据框中的值 - How do I replace the values in a dataframe based on a lookup table in another dataframe 如何根据条件替换熊猫数据框中的值? - How to replace values in a pandas dataframe based on conditions? 根据另一列的值替换Pandas数据框的Column的值 - Replace values of a Pandas dataframe's Column based on values of another column Pandas:根据条件用另一个数据帧值替换数据帧中的值 - Pandas: replace values in dataframe with another dataframes values based on condition 根据其他数据框替换熊猫数据框中的值 - Replace values in pandas dataframe based on other dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM