1

I have created the function below which returns a vector based on two input vectors. I have a pandas dataframe with different columns and I would like to apply that function to some columns of my dataframe (one pandas column would be the first vector of my function (nominal), another one the second vector of my function(CPI) and I would assign the result of my function to a new pandas column). How could I do it?

Thank you very much for your help,

Pierre

nominal=[10,10,10,10,10,10,10,10,10,10,10,10]
CPI=[0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02]

def nominal_to_real(nominal,CPI):
    real=[]
    product=1
    for i in range(len(nominal)):
        product*=(CPI[i]+1)**(1/12)     
        real.append(nominal[i]/product)
    return real
8
  • 2
    um, nominal_to_real(df.nominal, df.CPI)? But note, your function returns a list that has one more element than your input lists... so I'm not sure how you expect this to behave with a data-frame... Commented Mar 9, 2018 at 0:35
  • @juanpa.arrivillaga, I think (read hope) he's asking how to vectorize this, which is a fair question since there seems to be sum kind of cumproduct feature. Commented Mar 9, 2018 at 0:45
  • 1
    @Peslier53, what are you looking for as your end result? Commented Mar 9, 2018 at 0:47
  • Give a Minimal, Complete, Verifiable Example in your question. Commented Mar 9, 2018 at 1:56
  • @juanpa.arrivillaga Thank you very much! That worked perfectly! My list has indeed on more element than my inputs, my mistake. I just needed to delete real.append(nominal[0]) and everything is perfect. Commented Mar 9, 2018 at 9:23

1 Answer 1

1

You can use cumprod for vectorized solution:

nominal=[10,10,10,10,10,10,10,10,10,10,10,10]
CPI=[0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02]

def nominal_to_real(nominal,CPI):
    real=[]
    product=1
    for i in range(len(nominal)):
        product*=(CPI[i]+1)**(1/12)     
        real.append(nominal[i]/product)
    return real

a = nominal_to_real(nominal,CPI)
df = pd.DataFrame(np.c_[nominal, CPI, a], columns=['nominal','CPI','real'])

df['real1'] = df['nominal'] / ((df['CPI'] + 1)**(1/12)).cumprod() 
print (df)
    nominal   CPI      real     real1
0      10.0  0.02  9.983511  9.983511
1      10.0  0.02  9.967050  9.967050
2      10.0  0.02  9.950616  9.950616
3      10.0  0.02  9.934209  9.934209
4      10.0  0.02  9.917829  9.917829
5      10.0  0.02  9.901475  9.901475
6      10.0  0.02  9.885149  9.885149
7      10.0  0.02  9.868850  9.868850
8      10.0  0.02  9.852578  9.852578
9      10.0  0.02  9.836332  9.836332
10     10.0  0.02  9.820114  9.820114
11     10.0  0.02  9.803922  9.803922
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.