首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Pandas:添加具有来自其他数据帧的匹配行的索引的列

Pandas:添加具有来自其他数据帧的匹配行的索引的列
EN

Stack Overflow用户
提问于 2019-12-03 18:21:10
回答 2查看 12.8K关注 0票数 1

正在清理sharepoint列表,以便使用正确的表关系上载到mssql。

基本上,两个数据帧(数据、配置)都共享一些公共列(国家、业务)。我想要做的是在datadf中插入一个新列,其中每一行都包含configdf中匹配行的索引,该索引基于列country和business中的值。

数据帧数据:

代码语言:javascript
复制
-----|---------|----------|-----
 ... | Country | Business | ...
-----|---------|----------|-----
     |    A    |     1    |
-----|---------|----------|-----
     |    A    |     1    |
-----|---------|----------|-----
     |    A    |     2    |
-----|---------|----------|-----
     |    A    |     2    |
-----|---------|----------|-----
     |    B    |     1    |
-----|---------|----------|-----
     |    B    |     1    |
-----|---------|----------|-----
     |    B    |     2    |
-----|---------|----------|-----
     |    C    |     1    |
-----|---------|----------|-----
     |    C    |     2    |
-----|---------|----------|-----

数据帧配置(ID =索引):

代码语言:javascript
复制
----|---------|----------|-----
 ID | Country | Business | ...
----|---------|----------|-----
  1 |    A    |     1    |
----|---------|----------|-----
  2 |    A    |     2    |
----|---------|----------|-----
  3 |    B    |     1    |
----|---------|----------|-----
  4 |    B    |     2    |
----|---------|----------|-----
  5 |    C    |     1    |
----|---------|----------|-----
  6 |    C    |     2    |
----|---------|----------|-----

我想要添加到数据帧数据中的内容:

代码语言:javascript
复制
-----|---------|----------|-----------|-----
 ... | Country | Business | config_ID | ... 
-----|---------|----------|-----------|-----
     |    A    |     1    |     1     |
-----|---------|----------|-----------|-----
     |    A    |     1    |     1     |
-----|---------|----------|-----------|-----
     |    A    |     2    |     2     |
-----|---------|----------|-----------|-----
     |    A    |     2    |     2     |
-----|---------|----------|-----------|-----
     |    B    |     1    |     3     |
-----|---------|----------|-----------|-----
     |    B    |     1    |     3     |
-----|---------|----------|-----------|-----
     |    B    |     2    |     4     |
-----|---------|----------|-----------|-----
     |    C    |     1    |     5     |
-----|---------|----------|-----------|-----
     |    C    |     2    |     6     |
-----|---------|----------|-----------|-----

-找到了可以工作的东西

代码语言:javascript
复制
datadf['config_ID'] =  datadf.apply(lambda x: configdf[(configdf.country == x.country) & (configdf.business_unit == x.business_unit)].index[0], axis=1)

它完成了工作,尽管我愿意接受其他建议,特别是如果它可以与df.insert()一起使用的话。

EN

Stack Overflow用户

发布于 2019-12-03 19:58:04

这是一个使用pandas merge的解决方案。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html#pandas.DataFrame.merge

代码语言:javascript
复制
import pandas as pd

# make the two dataframes
data = pd.DataFrame({'Country':['A','A','A','A','B','B','B','C','C'],
                     'Business':[1,1,2,2,1,1,2,1,2]})

configdf = pd.DataFrame({'Country':['A','A','B','B','C','C'],
                         'Business':[1,2,1,2,1,2]})

# make a column with the index values
configdf.reset_index(inplace=True)

# merge the two dataframes based on the selected columns.
newdf = data.merge(configdf, on=['Country', 'Business'])
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59154836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档