0

I have a csv file which I have read with pandas. It contains a list of houses sold in Mumbai, with details like price, location, area etc. It looks like this:

       Price  Area  Location  ...  Gas_Connection  Jogging_Track  Swimming_Pool
0    4850000   720  Kharghar  ...           False          False          False
1    4500000   600  Kharghar  ...           False           True           True
2    6700000   650  Kharghar  ...           False           True           True
3    4500000   650  Kharghar  ...           False          False          False
4    5000000   665  Kharghar  ...           False          False          False

I want to perform operations on prices of houses in a particular locality. I there a way to use the prices of only the rows with Khargar as its locality? Just for some context there are about 6,500 entries in the df.

Thanks...

Aryagm
  • 530
  • 1
  • 7
  • 18

3 Answers3

1

if df is the data frame then this will give you a new dataframe with just those in the area Khargar

dfNew = df[df['Location’] =='Khargar']
Paul Brennan
  • 2,638
  • 4
  • 19
  • 26
1

You may want to try this:

    select_price = df.loc[df['Location'] == 'Kharghar']
    print (select_price)`
Panda
  • 23
  • 7
0

You can try this,

import pandas as pd
import numpy as np
df = merged = pd.DataFrame({'Location' : ["Kharghar", "Khargar","NC"],
                            'Price' : [100, 10, 20,]
                            })
idx = np.multiply(df['Location'] == "Kharghar",1)
prices = df['Price'][idx == 1]
print(prices)

The piece of code you need,

idx = np.multiply(df['Location'] == "Kharghar",1)
prices = df['Price'][idx == 1]
print(prices)

Or,

idx = df['Location'] == "Kharghar"
prices = df['Price'][idx == True]
print(prices)
Nott
  • 303
  • 1
  • 8
  • if you're not going to use the multiple built-in pandas slicing methods, I would recommend np.where or np.argwhere over this..you also don't need to mask a boolean array, you can just use it directly – Derek Eden Jan 02 '21 at 20:40
  • 1
    Thanks for your kind feedback. I generally use like "or option" to enhance readibility. – Nott Jan 02 '21 at 20:45