0

I want to add a new value for each loop in python pandas

        PRODUCT  PRICE
0    ABC       5000
1    ABB       2500
2    ABE       1800
3    DCB       7200

if df1 is like above,

for i in df1['PRICE']:
    if i>3000:
        PURCHASE = True
    else:
        PURCHESE = False

after the loop, I want to make the df1 like below

        PRODUCT  PRICE  PURCHSE
0    ABC       5000       True
1    ABB       2500       False
2    ABE       1800       False
3    DCB       7200       True

how would I do this?

0

2 Answers 2

2

The language of Pandas is somewhat similar to what you would use in a common conversation: the PURCHASE is when the PRICE > 3000.

df1['PURCHASE'] = df1['PRICE'] > 3000
Sign up to request clarification or add additional context in comments.

Comments

1

Could you please try following, using np.where here.

import pandas as pd
import numpy as np
df['PURCHASE']=np.where(df2.PRICE>3000,'TRUE','FALSE')

When we print the value of new df with new column named PURCHASE its value will be as follows.

    PRODUCT PRICE PURCHASE
0   ABC 5000    TRUE
1   ABB 2500    FALSE
2   ABE 1800    FALSE
3   DCB 7200    TRUE

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.