gpt4 book ai didi

python - Pandas 多索引 : Divide all columns by one column

转载 作者:太空狗 更新时间:2023-10-29 18:23:04 25 4
gpt4 key购买 nike

我有一个数据框 results 的形式

                  TOTEXPPQ      TOTEXPCQ     FINLWT21
year quarter
13 1 9.183392e+09 5.459961e+09 1271559.398
2 2.907887e+09 1.834126e+09 481169.672

我试图将所有(前两列)除以最后一列。我的尝试是

weights = results.pop('FINLWT21')
results/weights

但是我明白了

ValueError: cannot join with no level specified and no overlapping names

我不明白:索引中有重叠的名称:

weights.head()
year quarter
13 1 1271559.398
2 481169.672

是否有更好的方法来进行这种划分?我需要重置索引吗?

最佳答案

您必须指定分界线的轴(使用 div 方法):

In [11]: results.div(weights, axis=0)
Out[11]:
TOTEXPPQ TOTEXPCQ
year quarter
13 1 7222.149445 4293.909517
2 6043.371329 3811.807158

默认值为 axis=1,结果和权重的索引名称不重叠,因此出现错误消息。

关于python - Pandas 多索引 : Divide all columns by one column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29354553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com