python - 我需要比较两个 df 的匹配和不匹配,如果不匹配,我还需要确定哪个答案来自主 df
问题描述
我在 python 中有两个数据框,想比较两者以查找匹配项和不匹配项。重要的是,我可以在不匹配中确定哪个答案来自主答题纸,哪个答案来自用户答案。
我决定使用 pandas df.where 函数来实现这一点,除了能够识别哪个答案来自主答题纸以及哪个答案来自用户答案不匹配的情况外,它仍然有效。
# I have a DataFrame called df_master (master answer sheet)
import pandas as pd
df_master = pd.DataFrame({'B0': [1, 0, 0, 0, 0, 1],
'B1': [0, 0, 0, 0, 1, 0],
'B2': [0, 1, 0, 0, 0, 0],
'B3': [0, 0, 1, 0, 0, 0],
'B4': [0, 0, 0, 1, 0, 0]})
print(df_master)
# B0 B1 B2 B3 B4
# 0 1 0 0 0 0
# 1 0 0 1 0 0
# 2 0 0 0 1 0
# 3 0 0 0 0 1
# 4 0 1 0 0 0
# 5 1 0 0 0 0
# I also have a DataFrame called df_answers (users answers)
df_answers = pd.DataFrame({'B0': [0, 0, 0, 0, 0, 1],
'B1': [1, 0, 0, 0, 1, 0],
'B2': [0, 0, 0, 0, 0, 0],
'B3': [0, 1, 1, 0, 0, 0],
'B4': [0, 0, 0, 1, 0, 0]})
print(df_answers)
# B0 B1 B2 B3 B4
# 0 0 1 0 0 0
# 1 0 0 0 1 0
# 2 0 0 0 1 0
# 3 0 0 0 0 1
# 4 0 1 0 0 0
# 5 1 0 0 0 0
# when I compare the the two df's, for each match, matches correctly, where there
# is no match I have used other=2. However this is a problem as I cannot see which is
# the correct answer. It would be great if there was a way to work the code to reflect
# the master as a 3 and the incorrect answer from the users to stay 2?
comparison = df_master.where(df_master.values==df_answers.values, other=2)
print(comparison)
# My Results
# B0 B1 B2 B3 B4
# 0 2 2 0 0 0
# 1 0 0 2 2 0
# 2 0 0 0 1 0
# 3 0 0 0 0 1
# 4 0 1 0 0 0
# 5 1 0 0 0 0
# Expected Results
# B0 B1 B2 B3 B4
# 0 3 2 0 0 0
# 1 0 0 3 2 0
# 2 0 0 0 1 0
# 3 0 0 0 0 1
# 4 0 1 0 0 0
# 5 1 0 0 0 0
解决方案
在您使用replace
after str sum , ps 的情况下:您自己定义映射,例如 {'00':'both failed', '01': 'master failed'...}
(df_answers.astype(str)+df_master.astype(str)).replace({'00':0,'01':3,'10':2,'11':1})
Out[129]:
B0 B1 B2 B3 B4
0 3 2 0 0 0
1 0 0 3 2 0
2 0 0 0 1 0
3 0 0 0 0 1
4 0 1 0 0 0
5 1 0 0 0 0
推荐阅读
- typescript - TypeScript 中 Redux 操作的枚举
- hibernate - 从一个微服务调用一个方法到另一个微服务
- excel - “未找到方法或数据成员”错误消息
- angular - 取消订阅请求 Angular
- matrix - Power BI 自定义矩阵设计
- python - 当我尝试将 pat 作为列表传递时,了解 pandas.Series.str.replace 行为?
- mongodb - MongoDB - 如何从给定日期获取最近 30 天的日期,并且最后 30 天的日期应该是当前日期?
- wso2 - 如何在 WSO2 API Manager 上创建自定义页面?
- spring-boot - 我如何使用部署在战争 Spring Boot 应用程序中的 mysql jdbc 驱动程序
- php - 在文本文件中查找字符串,然后查找另一个字符串的第一个实例,然后获取后面的文本