[英]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
,与df2
和pivot
合并结果:
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.