正在清理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)
它完成了工作,尽管我愿意接受其他建议,尤其是如果它能与数据框插入()
你可以用纽比。在哪里函数来匹配数据帧
例如:
输出:
^{pr2}$编辑1:
好吧,那样的话,你可以用
基于示例数据帧datadf和configdf的输出:
下面是一个使用pandas merge的解决方案。在
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html#pandas.DataFrame.merge
相关问题 更多 >
编程相关推荐