首页 > 解决方案 > 如何将每一列乘以另一个df的所有列,获得多索引?

问题描述

我需要根据在另一个 df 中找到的百分比来拆分每一列。例如:

>>> import pandas as pd
>>> 
>>> things = ['some thing', 'another thing']
>>> 
>>> amount = pd.DataFrame({2019: [10, 20], 2020: [100, 200]}, index=things)
>>> amount
               2019  2020
some thing       10   100
another thing    20   200
>>> 
>>> split = pd.DataFrame({'first': [0.2, 0.9], 'second': [0.8, 0.1]}, index=things)
>>> split
               first  second
some thing       0.2     0.8
another thing    0.9     0.1
>>> 
>>> result = amount ??? split  # how to do this?
>>> result
               2019         2020       
              first second first second
some thing        2      8    20     80
another thing    18      2   180     20

我怎样才能在熊猫中一次简单地做到这一点?

标签: pythonpandasmulti-index

解决方案


您可以pd.concat()对来自 2 个数据帧的 (2 x 2) 系列的叉积使用列表推导,如下所示:

2 列表推导:

[amount[i] * split[j] for i in amount.columns for j in split.columns]对于 (2 x 2) 系列的叉积

[(x, y) for x in amount.columns for y in split.columns]对于列索引

result = pd.concat([amount[i] * split[j] for i in amount.columns for j in split.columns],
                   keys=[(x, y) for x in amount.columns for y in split.columns], axis=1)




print(result)

               2019          2020       
              first second  first second
some thing      2.0    8.0   20.0   80.0
another thing  18.0    2.0  180.0   20.0

如果您确定结果值将是整数并且希望这些值是整数,则可以通过以下方式进一步转换其类型.astype(int)

result = pd.concat([amount[i] * split[j] for i in amount.columns for j in split.columns],
                   keys=[(x, y) for x in amount.columns for y in split.columns], axis=1).astype(int)




print(result)

               2019         2020       
              first second first second
some thing        2      8    20     80
another thing    18      2   180     20

推荐阅读