[英]How to merge for two different rows in PANDAS?
I want to merge two dataframes.我想合并两个数据框。 The left dataframe has two identifiers, id1 and id2.
左边的数据帧有两个标识符,id1 和 id2。 The right dataframe has the string version of those identifiers.
正确的数据帧具有这些标识符的字符串版本。 What I want to do is get both ids and the string version of both ids in the same row.
我想要做的是在同一行中获取两个 ID 和两个 ID 的字符串版本。 Example:
例子:
left: right:
id1 id2 id string
0 1 0 "a"
3 4 1 "b"
10 0 3 "c"
1 4 4 "d"
10 "e"
Output of merging:合并输出:
id1 id2 string1 string2
0 1 "a" "b"
3 4 "c" "d"
10 0 "e" "a"
1 4 "b" "d"
How would I do this?我该怎么做?
Creating a mapper from the right
DataFrame is probably best here then using Series.map
on each column as it scales up very easily:从
right
DataFrame 创建映射器在这里可能是最好的,然后在每列上使用Series.map
因为它很容易扩展:
mapper = right.set_index('id')['string']
merged = left.copy()
for i, col in enumerate(merged.columns, 1):
merged[f'{mapper.name}{i}'] = merged[col].map(mapper)
Alternatively with chained merge
calls:或者使用链式
merge
调用:
merged = (
left.merge(right.rename(columns={'id': 'id1'}), on='id1', how='left')
.merge(right.rename(columns={'id': 'id2'}), on='id2', how='left',
suffixes=('1', '2'))
)
Both produce merged
:两者产生
merged
:
id1 id2 string1 string2
0 0 1 a b
1 3 4 c d
2 10 0 e a
3 1 4 b d
DataFrames:数据帧:
import pandas as pd
left = pd.DataFrame({
'id1': {0: 0, 1: 3, 2: 10, 3: 1},
'id2': {0: 1, 1: 4, 2: 0, 3: 4}
})
right = pd.DataFrame({
'id': {0: 0, 1: 1, 2: 3, 3: 4, 4: 10},
'string': {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.