Skip to content
Advertisement

Create new columns based on previous columns with multiplication

I want to create a list of columns where the new columns are based on previous columns times 1.5. It will roll until Year 2020. I tried to use previous and current but it didn’t work as expected. How can I make it work as expected?

df = pd.DataFrame({
         'us2000':[5,3,6,9,2,4],

}); df

a = []
for i in range(1, 21):
    a.append("us202" + str(i))
for previous, current in zip(a, a[1:]):
    df[current] = df[previous] * 1.5

Advertisement

Answer

IIUC you can fix you code with:

a = []
for i in range(0, 21):
    a.append(f'us20{i:02}')
for previous, current in zip(a, a[1:]):
    df[current] = df[previous] * 1.5

Another, vectorial, approach with numpy would be:

df2 = (pd.DataFrame(df['us2000'].to_numpy()[:,None]*1.5**np.arange(21),
                    columns=[f'us20{i:02}' for i in range(21)]))

output:

   us2000  us2001  us2002  us2003   us2004    us2005      us2006      us2007  ...
0       5     7.5   11.25  16.875  25.3125  37.96875   56.953125   85.429688   
1       3     4.5    6.75  10.125  15.1875  22.78125   34.171875   51.257812   
2       6     9.0   13.50  20.250  30.3750  45.56250   68.343750  102.515625   
3       9    13.5   20.25  30.375  45.5625  68.34375  102.515625  153.773438   
4       2     3.0    4.50   6.750  10.1250  15.18750   22.781250   34.171875   
5       4     6.0    9.00  13.500  20.2500  30.37500   45.562500   68.343750   
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement