python - 根据另一个数据帧对一个数据帧中的数据进行排序的最佳方法是什么?
问题描述
我有 2 个数据集,我正在尝试根据第二个数据集的值对一个数据集中的数据进行排序,并在第一个数据集中创建一个新列。如果数据集 1 和 2 中的值匹配,那么我想将新列填充为 true,否则为 false。在 Python 中执行此操作的最佳方法是什么?可能代码(如下所示)不起作用。
数据:
df1
Index ID type 1 type2
0 1 A.34 6.3 7.1
1 2 A.35 5.8 7.3
2 3 A.36 6.8 5.2
3 4 A.37 7.8 6.4
4 5 A.38 6.9 8.8
df2
Index ID Type 2
0 1 A.55 6.7
1 2 A.35 3.6
2 3 A.69 5.8
3 4 A.34 9.2
4 5 A.38 7.7
# Required Output
df3
Index ID type 1 type2 Status
0 1 A.34 6.3 7.1 bad
1 2 A.35 5.8 7.3 good
2 3 A.36 4.1 2.6 bad
3 4 A.37 7.8 6.4 bad
4 5 A.38 6.9 8.8 good
# The code I wrote is giving me ‘bad’ for all the rows:
Boolean = []
for x in df1.ID:
if x == x in df2.ID:
Boolean.append('good')
else:
Boolean.append('bad')
print (Boolean)
# Output obtained with code
Output:
['bad', 'bad', 'bad', 'bad', 'bad']
谢谢你。
解决方案
我认为这就是你要找的:
import pandas as pd
data1 = {
'Index': [1, 2, 3, 4, 5],
'ID': ['A.34', 'A.35', 'A.36', 'A.37', 'A.38'],
'type 1': [6.3, 5.8, 6.8, 7.8, 6.9],
'type2': [7.1, 7.3, 5.2, 6.4, 8.8]}
data2 = {
'Index': [1, 2, 3, 4, 5],
'ID': ['A.55', 'A.35', 'A.69', 'A.34', 'A.38'],
'Type 2': [6.7, 3.6, 5.8, 9.2, 7.7]}
df1 = pd.DataFrame(data=data1)
df2 = pd.DataFrame(data=data2)
merge_cols = ['Index', 'ID']
df = pd.merge(df1, df2[merge_cols], how='left', left_on=merge_cols, right_on=merge_cols, indicator=True)
d = {'left_only':'bad', 'both':'good'}
df['_merge'] = df['_merge'].map(d)
df.rename(columns={'_merge': 'Status'}, inplace=True)
df
输出df
如下所示:
Index ID type 1 type2 Status
0 1 A.34 6.3 7.1 bad
1 2 A.35 5.8 7.3 good
2 3 A.36 6.8 5.2 bad
3 4 A.37 7.8 6.4 bad
4 5 A.38 6.9 8.8 good
编辑:编辑以合并两列Index
和ID
推荐阅读
- r - 如何在闪亮的模块中将值从 ui 传递到服务器
- c# - 从 LINQ-to-Entities 表达式中的字符串中提取文本
- python - 当我创建具有使用颜色图着色的节点的网络时,由于 float() 参数导致的 TypeError
- php - 为什么我的 php 脚本在提交 post 请求时这么慢
- angular - npm install 后出错无法解析依赖关系
- node.js - 有没有办法在这个 reactjs 代码中获取这两个数组 ID?
- node.js - npm install discord.js 错误(npm ERR!代码1)
- r - 我正在尝试创建一个函数来计算向量中组内所有对组合的百分比差异
- c++ - 重载的提取运算符不会在 C++ 中成为朋友
- replace - NodeJS - 替换模块 - SyntaxError:意外的字符串