I am facing a problem. I have created a new column in my csv file and I am trying to add values in the column but don't know what functions to use. The column is called 'Discounted_Price' and I have to mentioned that for Ford and Chevrolet, the prices have a ten percent discount. This is the code up to this point:
CarPrices_discount=pd.read_csv('C:\\Users\\Jon\\Desktop\\data science\\car_prices.csv')
CarPrices_discount
CarPrices_discount.insert(15,'Discounted_Price',np.nan)
CarPrices_discount.head()
Ford_Chev=CarPrices_discount[(CarPrices_discount.make=='Ford')|(CarPrices_discount.make=='Chevrolet')]
#Ford_Chev['Discounted_Price']=Ford_Chev['sellingprice']*0.9
CarPrices_discount.loc[CarPrices_discount.Ford_Chev['Discounted_Price']=Ford_Chev['sellingprice']*0.9
I know the last line is wrong but I do not how to insert it with dataframe.loc.
CodePudding user response:
Data:
>>> import pandas as pd
>>> import numpy as np
>>> CarPrices_discount = pd.DataFrame({"make" : ['Chevrolet', 'a', 'b', 'Ford', 'Chevrolet', 'Ford'],
"sellingprice" : [900, 10,20,20,40,30]})
>>> CarPrices_discount
make sellingprice
0 Chevrolet 900
1 a 10
2 b 20
3 Ford 20
4 Chevrolet 40
5 Ford 30
>>> CarPrices_discount['Discounted_Price'] = CarPrices_discount.apply(lambda x:x.sellingprice*0.9 if x.make in ['Ford', 'Chevrolet'] else np.nan, axis=1)
>>> CarPrices_discount
make sellingprice Discounted_Price
0 Chevrolet 900 810.0
1 a 10 NaN
2 b 20 NaN
3 Ford 20 18.0
4 Chevrolet 40 36.0
5 Ford 30 27.0
CodePudding user response:
Ok, I found an answer of some sort but it is highly flawed:
Ford_Chev=CarPrices_discount[(CarPrices_discount.make=='Ford'(CarPrices_discount.make=='Chevrolet')] print(Ford_Chev)
Ford_Chev['Discounted_Price']=Ford_Chev["sellingprice"]*0.9
print(Ford_Chev)