简体   繁体   English

根据每一列的值索引行值

[英]Indexing Row Values Based on Value of Each Column

I have an 8r x 10c data frame and I want to duplicate the dataframe by dividing the values in each row by the first value in its column (ie 'indexing' each column, with the first value = 100). 我有一个8r x 10c的数据框,我想通过将每一行中的值除以其列中的第一个值来复制该数据框(即“索引”每列,第一个值= 100)。

So if I start with... 所以,如果我从...开始

ColA ColB ColC
1000 2000 3000
2000 3000 9000

It would return... 它会返回...

ColA ColB ColC
 100  100  100
 200  150  300

Is there a simple command to do this, or is it some sort of loop? 有一个简单的命令可以做到这一点,还是某种循环?

You could do the following: 您可以执行以下操作:

>>> df / (df.iloc[0] / 100)
   ColA ColB ColC
0   100  100  100
1   200  150  300

df.iloc[0] selects the first row. df.iloc[0]选择第一行。 Divide it by 100 to get a row of values to adjust each column by. 将其除以100可得到一行值以调整每一列。 Lastly we divide the entire DataFrame by this new row of values. 最后,我们将整个DataFrame除以新的值行。 Division happens along axis 0 by default (ie downwards along each column). 默认情况下,分割沿轴0进行(即,沿每列向下)。

An equivalent operation would be df / df.iloc[0] * 100 . 等效操作为df / df.iloc[0] * 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM