问题内容 来自以下数据框(df): |------------+--------------------+-------------| | child_code | child_nam
来自以下数据框(df
):
|------------+--------------------+-------------|
| child_code | child_name | parent_code |
|------------+--------------------+-------------|
| 900 | world | 0 |
| 920 | south-eastern asia | 900 |
| 702 | singapore | 920 |
|------------+--------------------+-------------|
我想生成这个数据框:
|------------+--------------------+-------------+--------------------|
| child_code | child_name | parent_code | parent_name |
|------------+--------------------+-------------+--------------------|
| 900 | World | 0 | |
| 920 | South-Eastern Asia | 900 | World |
| 702 | Singapore | 920 | South-Eastern Asia |
|------------+--------------------+-------------+--------------------|```
How could I make the equivalent of an MS excel `vlookup` to produce the `parent_name` column?
您可以使用series.map
:
import pandas as pd
import numpy as np
data = {'child_name': {0: 'World', 1: 'South-Eastern Asia', 2: 'Singapore'},
'child_code': {0: 900, 1: 920, 2: 702},
'parent_code': {0: 0, 1: 900, 2: 920}}
df = pd.DataFrame(data)
df['parent_name'] = df['parent_code'].map(df.set_index('child_code')['child_name'])
df
child_name child_code parent_code parent_name
0 World 900 0 NaN
1 South-Eastern Asia 920 900 World
2 Singapore 702 920 South-Eastern Asia
以上就是如何在单个数据帧内进行vlookup?的详细内容,更多请关注编程网其它相关文章!
--结束END--
本文标题: 如何在单个数据帧内进行vlookup?
本文链接: https://lsjlt.com/news/562452.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0