python - 如何将每一列乘以另一个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
我怎样才能在熊猫中一次简单地做到这一点?
解决方案
您可以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
推荐阅读
- javascript - 尝试在 mocha 测试中导出全局变量
- ruby-on-rails - RAILS 6:如何在 crontab 中运行 rails 任务?
- reverse - 反向地理编码 geopy 时出现“`address` must not be None”错误
- jquery - 通过 data 和 addClass 方法中的属性选择元素
- hl7-fhir - FHIR 调查问卷答复得出的观察结果
- r - 如何通过列表列表循环和子集
- report - flutter_driver 的报告工具
- react-native - 如何使用 react-native-share 共享多个文件
- python-3.6 - 如何找到或安装 libpython3.6m 和 python3.6m?
- c++ - c++ 复制构造函数和析构函数赋值