【发布时间】:2022-01-17 20:28:00
【问题描述】:
我已经使用以下脚本达到了一定程度,即使使用“is.in”这样的函数,我也无法完全理解它,可能是因为在此之前我从未使用过它。
输入df1:
ID Alternative ID
0 152503 009372
1 249774 249774
2 062005 196582
3 185704 185704
4 081231 081231
5 081231 062085
6 912568 222416
7 196782 195122
输入 df2:
New_ID
0 498109
1 081231
2 231051
3 062005
4 152503
5 967272
6 875612
我的想法是我想检查“ID”的值是否与 df1 中的“Alternative ID”上的值匹配。如果他们这样做,它应该分别在名为“Result_1”和“Result_2”的两个新列上返回“Match”和“Correct”。对于那些不匹配的,查找它们是否全部出现在 df2 的“New_ID”列中。如果它们在上面提到的那两列上分别返回“新匹配”和“好”的值。如果它们不存在,则返回“不匹配”和“错误”。
对于这个任务的第一部分,这是我使用的代码:
def compl(df1):
if (df1['ID'] == df1['Alternative ID']):
return 'Match', 'Correct'
elif (df1['ID'] != df1['ID']):
这里找不到下一步基本上检查不匹配的值是否在df2等中。
df1[['Result_1', 'Result_2']] = df1.apply(compl, axis = 1, result_type = 'expand')
理想的输出 ->
ID Alternative ID Result_1 Result_2
0 152503 009372 NEW Match Good
1 249774 249774 Match Correct
2 062005 196582 NEW Match Good
3 185704 185704 Match Correct
4 081231 062085 Match Correct
5 912568 222416 Not Match Error
6 196782 195122 Not Match Error
任何建议/方法将不胜感激
【问题讨论】:
标签: python pandas dataframe numpy