将多索引 (axis=1) 数据框中的特定列除以某些其他列
我有一个 pd.DataFrame ,它具有以下结构的列多重索引:
a1 a1 a1 a2 a2 a2 a3 a3 a3 ...
b1 b2 b3 b1 b2 b3 b1 b2 b3 ...
row1 values ...
row2
row3
...
我想将每个 a 组除以其相应的 b3 列中的最后一个元素。 即用 a1-b1 列除以 a1-b3 中的最后一个值,对于 a1-b2 和 a1-b3 也是如此(这样 a1-b3 的最后一个元素实际上是 1)。对于下一组 a2,同样的类比,但将其列除以 a2-b3 中的最后一个值。等等...
我正在寻找一个简单的表达式来做到这一点。我有一个非常繁琐的代码,我将数据帧分成存储在字典中的组,然后分别划分每个组并将它们合并回一起,但不可能是这样,对吧?
谢谢!
最好的,JZ
I have a pd.DataFrame which has a column multiindex of the following structure:
a1 a1 a1 a2 a2 a2 a3 a3 a3 ...
b1 b2 b3 b1 b2 b3 b1 b2 b3 ...
row1 values ...
row2
row3
...
I would like to divide each a-group by the last element in its corresponding b3 column.
I.e. divide the column a1-b1 by last value in a1-b3, same for a1-b2 and a1-b3 (such that the last element of a1-b3 will actually be 1). for the next group a2, same analogy but dividing its columns by last value in a2-b3. and so on...
I am looking for a simple expression to do this. I have a very cumbersome code where I split up the dataframe into groups that are stored in a dict and then divide each group respectively and merge them back together, but that cant be it, right?
Thanks!
Best, JZ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
groupby
:设置:
Use
groupby
:Setup: