简体   繁体   中英

Calculate value using previously-calculated value (from the same column) and value from another column in a Pandas Dataframe

After hours trying to learn how to do this, I'm reaching out to the community.

I'm starting with the following:

                perf
date                
2018-06-01  0.012923
2018-06-02  0.039364
2018-06-03  0.042805
2018-06-04 -0.033214
2018-06-05 -0.021745

Need to calculate a cumulative percentage change on a new column but need to ensure the calculation uses 100 as the starting value. So I prepend a single row with the 100:

                perf  pct_change
date                            
2018-05-31       NaN       100.0
2018-06-01  0.012923         NaN
2018-06-02  0.039364         NaN
2018-06-03  0.042805         NaN
2018-06-04 -0.033214         NaN

What I need to get is this:

                perf  pct_change
date                            
2018-05-31       NaN       100.0
2018-06-01  0.012923    101.2923
2018-06-02  0.039364 105.2795701
2018-06-03  0.042805 109.7860621
2018-06-04 -0.033214 106.1396278

The formula being something like pct_change = previous_days_pct_change * ( 1 + perf )

I tried a few different approaches including a for ... in loop with no success.

# INCOMPLETE/DOES NOT WORK (adding for illustration purposes only)
for index, row in performance.iterrows():
    curr = performance.loc[index, 'perf']
    pidx = index + pd.DateOffset(-1)
    prev = performance.iloc[[pidx], 'pct_change']
    performance.loc[index, 'pct_change'] = prev * ( 1 + curr )

I also tried:

performance['pct_change'] = performance['pct_change'].shift() * ( 1 + performance['perf'] )

Which yields:

                perf  pct_change
date                            
2018-05-31       NaN         NaN
2018-06-01  0.012923  101.292251
2018-06-02  0.039364         NaN
2018-06-03  0.042805         NaN
2018-06-04 -0.033214         NaN

But that only gives me the one value.

I suspect there is already a much simpler way to do what I'm trying to do but I'm just not finding it. Any help would be appreciated. Very easy to do in a spreadsheet but I want to learn how to do this in Pandas.

Thank you

Using cumprod :

df['pct_change'] = (df['perf']+1).cumprod() * 100

achieves what you actually want:

pct_change_0 = (perf_0 + 1) * 100
pct_change_1 = pct_change_0 * (perf_1 + 1) = (perf_0 + 1) * (perf_1 + 1) *  100
pct_change_2 = pct_change_1 * (perf_2 + 1) = (perf_0 + 1) * (perf_1 + 1) * (perf_2 + 1) * 100
...

So you are actually computing the cumulative product of perf values (or to be more accurate perf + 1 values).

Like so:

dates = ['2018-06-01', '2018-06-02', '2018-06-03', '2018-06-04', '2018-06-05']
import datetime as dt
dates = [pd.datetime.date(dt.datetime.strptime(x, "%Y-%m-%d")) for x in dates]
perfs = [0.012923, 0.039364, 0.042805, -0.033214, -0.021745]
df = pd.DataFrame({'perf': perfs}, index=dates)

# The important bit:
df['pct_change'] = ((df['perf'] + 1).cumprod() * 100)

df
#                 perf  pct_change
# 2018-06-01  0.012923  101.292300
# 2018-06-02  0.039364  105.279570
# 2018-06-03  0.042805  109.786062
# 2018-06-04 -0.033214  106.139628
# 2018-06-05 -0.021745  103.831622

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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