Jack
Jack

Reputation: 75

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

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          30          40
1    10       5          10          15          20
2    15      25          50          75          75

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

Upvotes: 0

Views: 95

Answers (3)

Corralien
Corralien

Reputation: 120409

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

Upvotes: 0

Renaud
Renaud

Reputation: 2819

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

Upvotes: 1

1sixunhuit
1sixunhuit

Reputation: 62

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

Upvotes: 2

Related Questions