从熊猫数据框中选择特定行

时间:2021-01-02 20:07:22

标签: python pandas

我有一个用熊猫阅读的 csv 文件。它包含在孟买出售的房屋清单,包括价格、位置、面积等详细信息。它看起来像这样:

       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

我想对特定地区的房屋价格进行操作。我有没有办法只使用 Khargar 作为其位置的行的价格?仅就某些上下文而言,df 中大约有 6,500 个条目。

谢谢...

3 个答案:

答案 0 :(得分:2)

如果 df 是数据框,那么这将为您提供一个新的数据框,其中仅包含 Khargar 区域中的数据框

collect_set

答案 1 :(得分:1)

你可能想试试这个:

    select_price = df.loc[df['Location'] == 'Kharghar']
    print (select_price)`

答案 2 :(得分:0)

你可以试试这个,

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)

你需要的一段代码,

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

或者,

idx = df['Location'] == "Kharghar"
prices = df['Price'][idx == True]
print(prices)