Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

python - Accessing Previous Entry of a Pandas DataFrame / Altering a variable during vectorized operations

I have the following Pandas data frame that looks like this:

           position      price  transaction     portfolio
date                                                      
2015-02-09       buy  29.530001 -5000.000000  10000.000000
2015-03-23      sell  25.520000  4321.029439   9321.029439
2015-04-09       buy  28.100000 -5000.000000   9321.029439
2015-07-09      sell  24.110001  4290.035638   9290.035638
2015-07-16       buy  25.240000 -5000.000000   9290.035638

I am attempting to keep track of my portfolio value at each buying/selling position, but I cannot seem to figure out how to do such with built-in pandas functions.

To paint a clearer picture, every entry with a position of "buy" will result in purchase of $5,000 worth of stock, as indicated by the transaction column. Of course, this action will not alter the overall portfolio value, as the money has simply been moved to stock. However, every entry with a position of "sell" will result in an alteration of the overall portfolio value, as the selling price could be higher or lower than the price which it was purchased for.

So, what my question boils down too is, can I still used vectorized pandas operations/functions to accurately keep track of my portfolio value at each "step" or entry in the data frame. According to the earlier snippet of my data frame, the desire output would be:

           positions      price  transaction     portfolio
date                                                      
2015-02-09       buy  29.530001 -5000.000000  10000.000000
2015-03-23      sell  25.520000  4321.029439   9321.029439
2015-04-09       buy  28.100000 -5000.000000   9321.029439
2015-07-09      sell  24.110001  4290.035638   8611.065204
2015-07-16       buy  25.240000 -5000.000000   8611.065204

Lastly, the code that is currently producing this output is:

# start with 10,000$
count = 10000

# buy n amount worth of stock on every buy position
amount = 5000

# set portfolio value
data_Frame['portfolio'] = count

# alter portfolio value based on selling position first
data_Frame.loc[data_Frame['positions'] == 'sell', 'portfolio'] = data_Frame['portfolio'].shift() + (data_Frame['transaction']-5000)

# reset portfolio value 
data_Frame.loc[data_Frame['positions'] == 'buy', 'portfolio'] = data_Frame['portfolio'].shift()

# reset first entry for clean-look
data_Frame['portfolio'].iloc[0] = count 

If possible, I would like to avoid having to use a for loop. I have also thought about defining a function and using the pandas apply() or map() functions to then only apply it to the individual column.

question from:https://stackoverflow.com/questions/65863063/accessing-previous-entry-of-a-pandas-dataframe-altering-a-variable-during-vect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...