Home > Mobile >  Create new columns based on previous columns with multiplication
Create new columns based on previous columns with multiplication

Time:03-03

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

CodePudding user response:

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   

CodePudding user response:

Try:

for i in range(1, 21):
    df[f"us{int(2000 i):2d}"] = df[f"us{int(2000 i-1):2d}"].mul(1.5)

>>> df
   us2000  us2001  us2002  ...       us2018       us2019        us2020
0       5     7.5   11.25  ...   7389.45940  11084.18910  16626.283650
1       3     4.5    6.75  ...   4433.67564   6650.51346   9975.770190
2       6     9.0   13.50  ...   8867.35128  13301.02692  19951.540380
3       9    13.5   20.25  ...  13301.02692  19951.54038  29927.310571
4       2     3.0    4.50  ...   2955.78376   4433.67564   6650.513460
5       4     6.0    9.00  ...   5911.56752   8867.35128  13301.026920

[6 rows x 21 columns]
  • Related