python - 将一个数据帧中一行的值设置为另一个数据帧的一行中包含的值
问题描述
我试图通过将一个数据帧中的一行的值设置为另一个数据帧中的一行的值来降低计算的复杂性。我要填充的空数据框是单索引,包含值的数据框是多索引。我的目标是执行一个类似 excel 的函数,我可以将包含在一个表中的多个单元格的值设置为等于包含在另一个表中的单元格。
df 如下所示:
*多指标
___________________________________________________________________________________________
|Chan. | Duration | Designation| Manufact. |Total Purchases| Sales | Cost |
|______|____________|____________|______________|_______________|_____________|_____________|
| | Month | Special | Brand | 153 | 5613.65 | 4756.46 |
| | | |______________|_______________|_____________|_____________|
| | | | Generic | 1567 | 16546.07 | 16000.00 |
|Retail|____________|____________|______________|_______________|_____________|_____________|
| | Season |Not Special | Brand | 351 | 13246.00 | 15086.26 |
| | | |______________|_______________|_____________|_____________|
| | | | Generic | 7653 | 165163.03 | 150065.41 |
|______|____________|____________|______________|_______________|_____________|_____________|
*单一索引
____________________________________________________________________________
| Index | Total Purchases | Sales | Cost |
|__________________________________|__________________|__________|_________|
| Retail Month Special Brand | <NA> | <NA> | <NA> |
|__________________________________|__________________|__________|_________|
| Retail Month Special Generic | <NA> | <NA> | <NA> |
|__________________________________|__________________|__________|_________|
| Retail Season Not Special Brand | <NA> | <NA> | <NA> |
|__________________________________|__________________|__________|_________|
| Retail Season Not Special Generic| <NA> | <NA> | <NA> |
|__________________________________|__________________|__________|_________|
我想将第一个数据帧中的值填充到第二个数据帧中。为此,我尝试了以下解决方案:
df.loc[('Retail Month Special Brand'), ['Total Purchases', 'Sales', 'Cost']] = multi_index.loc[('Retail', 'Month', 'Special', 'Brand'), ['Total Purchases', 'Sales', 'Cost']].values()
最终结果应如下所示:
____________________________________________________________________________
| Index | Total Purchases | Sales | Cost |
|__________________________________|__________________|__________|_________|
| Retail Month Special Brand | 153 | 5613.65 | 4756.46 |
|__________________________________|__________________|__________|_________|
但是我收到了以下信息:
TypeError: 'numpy.ndarray' object is not callable.
解决方案
推荐阅读
- python - 使用 glOrtho 时如何使用光照?
- mysql - 从另一个表中不存在主键作为外键的表中删除
- r - 在 R 中的数据框中添加多个排名列
- docker - Azure Dev Ops 管道 (ADO) 中的 docker-compose
- php - PHP SimpleXML 无法加载解压缩的 docx XML
- javascript - 在页面上动态添加 Facebook 元数据
- image-processing - 我怎样才能进一步改进这个模型(自动编码器)?
- java - ResolutionException: Unable to resolve root: 缺少需求 [root] filter:="(osgi.wiring.package=javax.jws)(version>=1.1.0)(!(version>=2.0.0))
- algorithm - 分析用户网站访问模式时间和空间复杂度
- r - 有没有办法获取 R 数据帧的第一个最小值?