0

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

1
  • I think your expect outcome is wrong Commented Sep 23, 2021 at 20:38

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

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.