正在清理sharepoint列表,以便使用正确的表关系上载到mssql。
基本上,两个数据帧(数据、配置)都共享一些公共列(国家、业务)。我想要做的是在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 =索引):
----|---------|----------|-----
ID | Country | Business | ...
----|---------|----------|-----
1 | A | 1 |
----|---------|----------|-----
2 | A | 2 |
----|---------|----------|-----
3 | B | 1 |
----|---------|----------|-----
4 | B | 2 |
----|---------|----------|-----
5 | C | 1 |
----|---------|----------|-----
6 | C | 2 |
----|---------|----------|-----我想要添加到数据帧数据中的内容:
-----|---------|----------|-----------|-----
... | 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 |
-----|---------|----------|-----------|------找到了可以工作的东西
datadf['config_ID'] = datadf.apply(lambda x: configdf[(configdf.country == x.country) & (configdf.business_unit == x.business_unit)].index[0], axis=1)它完成了工作,尽管我愿意接受其他建议,特别是如果它可以与df.insert()一起使用的话。
发布于 2019-12-03 19:58:04
这是一个使用pandas 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'])https://stackoverflow.com/questions/59154836
复制相似问题