pandas 按第一行划分数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12007406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:47:48  来源:igfitidea点击:

Divide DataFrame by first row

pythonpandas

提问by bigsleep

I have checked the documentation. I don't understand the way to index a Pandas DataFrame.

我已经检查了文档。我不明白索引 Pandas DataFrame 的方法。

I would like to divide a DataFrame of stock prices by their respective initial values to index the different stocks to 100. I want to compare their performance. The DataFrame looks like this:

我想将股票价格的 DataFrame 除以它们各自的初始值,以将不同的股票索引为 100。我想比较它们的表现。数据框看起来像这样:

>>> IndexPrices
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 157 entries, 1999-12-31 00:00:00 to 2012-12-31 00:00:00
Freq: M
Data columns:
MSCI WORLD :G U$                        148  non-null values
S&P 500 COMPOSITE                       148  non-null values
DAX 30 PERFORMANCE                      148  non-null values
RUSSELL 2000                            148  non-null values
FTSE 100                                148  non-null values
US Treasury Bond Yields 30 Year Bond    148  non-null values
dtypes: float64(6)

I have stuff like this, but it's not getting me anywhere.

我有这样的东西,但它没有让我去任何地方。

IndexPrices.divide(IndexPrices[0:1])

回答by Wouter Overmeire

In [193]: df
Out[193]:
   A  B  C  D
a  1  8  9  1
b  5  4  3  6
c  4  6  1  3
d  1  0  2  9

In [194]: df.divide(df.ix[0] / 100)
Out[194]:
     A    B           C    D
a  100  100  100.000000  100
b  500   50   33.333333  600
c  400   75   11.111111  300
d  100    0   22.222222  900