Pandas:添加索引为来自其他datafram的匹配行的列

2024-10-04 11:25:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

数据帧数据:

-----|---------|----------|-----
 ... | Country | Business | ...
-----|---------|----------|-----
     |    A    |     1    |
-----|---------|----------|-----
     |    A    |     1    |
-----|---------|----------|-----
     |    A    |     2    |
-----|---------|----------|-----
     |    A    |     2    |
-----|---------|----------|-----
     |    B    |     1    |
-----|---------|----------|-----
     |    B    |     1    |
-----|---------|----------|-----
     |    B    |     2    |
-----|---------|----------|-----
     |    C    |     1    |
-----|---------|----------|-----
     |    C    |     2    |
-----|---------|----------|-----

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

^{pr2}$

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

^{3}$

----找到了有用的东西----

datadf['config_ID'] =  datadf.apply(lambda x: configdf[(configdf.country == x.country) & (configdf.business_unit == x.business_unit)].index[0], axis=1)

它完成了工作,尽管我愿意接受其他建议,尤其是如果它能与数据框插入()


Tags: 数据idconfig列表data关系unitbusiness
2条回答

你可以用纽比。在哪里函数来匹配数据帧

例如:

datadf = pd.DataFrame([['USA','Business1'],['AUS','Business2'],['UK','Business3'],['IND','Business4']],
                          columns=['country','business'])
configdf = pd.DataFrame([['AUS','Business2'],['IND','Business4'],['USA','Business1'],['UK','Business3']],
                          columns=['country','business'])

datadf['new_col'] = datadf.apply(lambda x: (np.where(x == configdf)[0][0]),axis=1)
print(datadf)

输出:

^{pr2}$

编辑1:

好吧,那样的话,你可以用

datadf['new_col'] = datadf.apply(lambda x: (np.where((x['country'] == configdf['country']) & (x['business'] == configdf['business']))[0][0]),axis=1)

基于示例数据帧datadf和configdf的输出:

  country business  new_col
0       A        1        0
1       A        1        0
2       A        2        1
3       A        2        1
4       B        1        2
5       B        1        2
6       B        2        3
7       C        1        4
8       C        2        5

下面是一个使用pandas merge的解决方案。在

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

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'])

相关问题 更多 >