Home > Net >  Creating multiple additional calculated columns to a dataframe in pandas using a for statement
Creating multiple additional calculated columns to a dataframe in pandas using a for statement

Time:09-24

I am trying to create an additional column to my dataframe using a loop, where the additional columns would be a multiple of a current column, but that multiple will change. I understand that this can be solved relatively easily by just creating a new column for each, but this is part of a larger project that I cant do that for.

Starting with this dataframe:

   Time  Amount
0    20      10
1    10       5
2    15      25

Hoping for the following outcome:

       Time  Amount  Amount i=2  Amount i=3  Amount i=4
0    20      10          20          20          20
1    10       5          10          10          10
2    15      25          50          50          50

Think there should be an easy answer, but cant find anything online. So far I have this:

data = {'Time':  [20,10,15],
     'Amount': [10,5,25]}    
df = pd.DataFrame(data)

for i in range(2,5):
    df = df.append(df['Amount']*i)
    print(df)

Thanks

CodePudding user response:

Do you want something like this ?

for i in range(2,5): 
    df["Amout i={}".format(i)] = df['Amount']*i 

Output :

   Time  Amount  Amout i=2  Amout i=3  Amout i=4
0    20      10         20         30         40
1    10       5         10         15         20
2    15      25         50         75        100

CodePudding user response:

Are you looking for:

import pandas as pd


data={'Time':[20,10,15],'Amount':[10,5,25]}

df=pd.DataFrame(data)

for i in range(2,5):
    df['Amount i=' str(i)]=df['Amount']*i
print(df)

Result:

   Time  Amount  Amount i=2  Amount i=3  Amount i=4
0    20      10          20          30          40
1    10       5          10          15          20
2    15      25          50          75         100

CodePudding user response:

Try:

df = df.join(pd.concat([df['Amount'].mul(i).rename(f'Amount i={i}')
                            for i in range(2, 5)], axis=1))
>>> df
   Time  Amount  Amount i=2  Amount i=3  Amount i=4
0    20      10          20          30          40
1    10       5          10          15          20
2    15      25          50          75         100
  • Related