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?
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.