Selecting specific rows from a pandas data frame

Question:

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…

Asked By: Arya Man

||

Answers:

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']
Answered By: Paul Brennan

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)
Answered By: Nott

You may want to try this:

    select_price = df.loc[df['Location'] == 'Kharghar']
    print (select_price)`
Answered By: Panda
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.