Start function in first empty cell per column - Python

Asked

Viewed 26 times

0

I have a portfolio with some shares that I called wallet. I’m calculating the normalized returns on this portfolio by simply dividing the current price of each share by the initial price (base date). However, since the shares were purchased at different dates, some shares have NA value at the beginning of the period.

If all were purchased on the same date I would do the following:

carteira_rb_norm = carteira_rb.copy()
for i in carteira_rb_norm.columns[1:]:
  carteira_rb_norm[i] = carteira_rb_norm[i] / carteira_rb_norm[i][0]

where the [0] indicates that I am dividing the daily prices of each share by the available price in the first row.

However, I need the initial price of each share to be the first non-zero value. The index of these values are as follows::

BSEV3.SA 56

CIEL3.SA 63

GUAR3.SA 46

PCAR3.SA 142

PETR4.SA 0

SMLS3.SA 61

UGPA3.SA 142

That I got through the function carteira_rb.isnull().sum()

In short, I would need each action (column of my dataframe) to have its normalized value from the date it presents available price. For example, for BSEV3.SA, all values of line 56 down (including itself) would be divided by Biosev’s price on line 56. In the case of CIEL3.SA, the division would start on line 63.

  • 1

    It would help if you put the df you want as the final result, as well as put the data (csv) or a sample of them.

  • 2

    If you show the dataframe you have, preferably post a way to create an equal dataframe for those who answer, and how you want the answer, you can answer. The way it is, just magical. "line 56 down" ? Which line 56? no one appears.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.