首页 > 解决方案 > 将一个数据帧中一行的值设置为另一个数据帧的一行中包含的值

问题描述

我试图通过将一个数据帧中的一行的值设置为另一个数据帧中的一行的值来降低计算的复杂性。我要填充的空数据框是单索引,包含值的数据框是多索引。我的目标是执行一个类似 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.

标签: pythonpandasdataframe

解决方案


推荐阅读