user8967185 :
I want to add another column RevisedPrice based on mentioned condition
if Price=1500 then RevisedPrice=Price+(Price*0.15) else if Price=800 then RevisedPrice=Price+(Price*0.10) else RevisedPrice=Price+(Price*0.5)
Below is my code------------
df['RevisedPrice']=[x+(0.15*x) if x==1500 else x+(0.10*x) if x==800 else x+(0.05*x) for x in df['Price']]
i am getting my column value as RevisedPrice=Price+(Price*0.5)
Shubham Sharma :
You can use the apply
function of pandas dataframe to achieve the desired result. Here is the code you might want to try:
def transform(row):
if row["Price"] == 1500:
scale = 0.15
elif row["Price"] == 800:
scale = 0.10
else:
scale = 0.5
return row["Price"] + row["Price"] * scale
df["RevisedPrice"] = df.apply(transform, axis=1)
And when you execute >>>print(df.head())
OUTPUT:
Date Event Price RevisedPrice
0 11/8/2011 Music 1500 1725.0
1 11/9/2011 Poetry 800 880.0
2 11/10/2011 Music 1500 1725.0
3 11/11/2011 Comedy 1200 1800.0
4 11/12/2011 Poetry 800 880.0