1

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'])

Yuca
  • 6,010
  • 3
  • 22
  • 42
Jaydeep
  • 21
  • 1
  • 3
  • There is nothing wrong with your code. Please supply a [mcve]. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) if you need help with this. – jpp Oct 08 '18 at 14:31

1 Answers1

2

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)
quest
  • 3,576
  • 2
  • 16
  • 26