[英]Pandas: Create column in dataframe and assign value to the column by looking into another dataframe
[英]Find matching value in column and create another column pandas dataframe
假设我有以下 dataframe:
ID Country Employee Location
1 AE Jay AAA
2 AE Mary aa
3 AE Peter bbb
3 AE Peter ddd
6 DK Donk ddd
7 CZ Cesar fff
7 CZ Cesar GGg
7 CZ Cesar
8 CZ Carlos #
我需要使用下面的 dataframe 来确认位置值是否有效(根据他们的国家/地区)并创建一个名为“旧位置名称”的额外列,其中包含以下内容:
如果值与查找 dataframe 匹配(无论是否大写),添加到“旧位置名称”列“正确值”
如果 Location 的值不正确,请将先前在“Location”列中使用的值添加到“Legacy Location Name”,并在“Location”中添加查找 dataframe 的现有位置的第一个值
如果 Location 的值为空(如倒数第二行),将值“LOCATION NOT PROVIDED”添加到“Legacy Location Name”,并在“Location”中添加查找 dataframe 的现有位置的第一个值
查找df:
Country Location
AE bbb
AE aaa
AE ccc
DK ddd
DK eee
DK fff
CZ ggg
CZ hhh
Output 预计
ID Country Employee Location Legacy Location
1 AE Jay AAA CORRECT VALUE
2 AE Mary bbb aa
3 AE Peter bbb CORRECT VALUE
3 AE Peter bbb ddd
6 DK Donk ddd CORRECT VALUE
7 CZ Cesar ggg fff
7 CZ Cesar GGg CORRECT VALUE
7 CZ Cesar LOCATION NOT PROVIDED
8 CZ Carlos ggg #
实现它的最佳方法是什么?
谢谢!
并不复杂,但需要很多步骤:
s = (lookup_df.drop_duplicates('Country')
.set_index('Country')['Location']
)
out = (df
# handle location independently of case
.assign(Location=df['Location'].str.casefold())
# identify the correct values by merging
.merge(lookup_df.assign(**{'Legacy Location': 'CORRECT VALUE'}),
how='left')
# replace invalid locations
.assign(**{'Location': lambda d: df['Location'].mask(d['Legacy Location'].isna()).fillna(df['Country'].map(s).mask(df['Location'].isna())),
# add previous invalid locations
'Legacy Location': lambda d: d['Legacy Location'].fillna(df['Location'].fillna('LOCATION NOT PROVIDED'))})
)
print(out)
注意。 为简单起见,假设所有空单元格都是 NaN。
Output:
ID Country Employee Location Legacy Location
0 1 AE Jay AAA CORRECT VALUE
1 2 AE Mary bbb aa
2 3 AE Peter bbb CORRECT VALUE
3 3 AE Peter bbb ddd
4 6 DK Donk ddd CORRECT VALUE
5 7 CZ Cesar ggg fff
6 7 CZ Cesar GGg CORRECT VALUE
7 7 CZ Cesar NaN LOCATION NOT PROVIDED
8 8 CZ Carlos ggg #
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.