I have checked the documentation. I don't understand the way to index a 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:
>>> 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])
DataFrame elements can be divided by a pandas series or by a Python sequence as well. Calling div() on a DataFrame instance is equivalent to invoking the division operator (/). The div() method provides the fill_value parameter which is used for replacing the np.
Method 2: Pandas divide two columns using div() function It divides the columns elementwise. It accepts a scalar value, series, or dataframe as an argument for dividing with the axis. If the axis is 0 the division is done row-wise and if the axis is 1 then division is done column-wise.
Using the iloc() function to split DataFrame in Python We can use the iloc() function to slice DataFrames into smaller DataFrames. The iloc() function allows us to access elements based on the index of rows and columns. Using this function, we can split a DataFrame based on rows or columns.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With