python - Assigning dataframe id based on id in another dataframe
问题描述
I have two dataframes called dataframe A and dataframe B
A
id| column1 |column2
3 | 439 | 2398
41| 498 | 34
2 | 233 | 43
B
column1 |column2
439 | 2398
498 | 56
233 | 43
I would like to assign an id column to dataframe B based on the ids in dataframe A. If a row is present in dataframe A and in dataframe B, I would like to assign the corresponding id from dataframe A to dataframe B. If a row in dataframe B is not in dataframe A I would like to assign "None" to that cell in the id column in B. That is, I would like to have
B
id | column1 |column2
3 | 439 | 2398
None| 498 | 56
2 | 233 | 43
Question: How do I assign the ids from dataframe A to dataframe B based on the columns in dataframe B?
解决方案
You can use a left merge. Note for a numeric series NaN
values will force id
series to become of type float
. In most situations this isn't a problem, and I wouldn't advise you to "force" type conversion row-by-row.
res = pd.merge(B, A, how='left')
print(res)
column1 column2 id
0 439 2398 3.0
1 498 56 NaN
2 233 43 2.0
推荐阅读
- java - Spring 中 Swagger2 的基本身份验证 - XML 配置
- java - 如何获取 Flink 中记录的 Kafka 时间戳?
- python - 在不同的表上使用多个 QSqlQueryModel 时 QSqlQueryModel::clear() 崩溃
- oculus - 已安装 Oculus Go 移动 SDK,缺少 VrSamples
- javascript - React Native - 每次孩子设置状态时渲染更新(我不想)
- c# - 使用 c# 和 sqlserver 保存在 gridview 中输入的数据
- ios - 如何在 iOS swift3 中将模型转换为数组
- python - 获取有关数据类字段的类型信息
- julia - Julia 相当于 movmean(array,window,dims)?
- java - 选择的事务策略需要访问 JTA TransactionManager