I want to create a new column in dataframe by taking a ratio of two existing columns. Following code works but it does not retain the column df[price_per_sqft].
df['price_per_sqft'] = (df['SalePrice']/df['LotArea'])
I want to create a new column in dataframe by taking a ratio of two existing columns. Following code works but it does not retain the column df[price_per_sqft].
df['price_per_sqft'] = (df['SalePrice']/df['LotArea'])
This should do:
df['price_per_sqft'] = df['SalePrice']/df['LotArea']
or you can use pd.assign
df.assign(price_per_sqft = df.SalePrice/df.LotArea)