簡體   English   中英

如何通過為每列選擇特定范圍來消除 dataframe 中的行? - Pandas

[英]How to eliminate rows in a dataframe by selecting a specific range for each column? - Pandas

我正在研究顯示巴西房地產租賃信息的 dataframe。 這是數據集的示例:

data = {
    'city': ['São Paulo', 'Rio', 'Recife'],
    'area(m2)': [90, 120, 60],
    'Rooms': [3, 2, 4],
    'Bathrooms': [2, 3, 3],
    'animal': ['accept', 'do not accept', 'accept'],
    'rent($)': [2000, 3000, 800]}

df = pd.DataFrame(
    data,
    columns=['city', 'area(m2)', 'Rooms', 'Bathrooms', 'animal', 'rent($)'])

print(df)

這是示例的外觀:

        city  area(m2)  Rooms  Bathrooms         animal  rent($)
0  São Paulo        90      3          2         accept     2000
1        Rio       120      2          3  do not accept     3000
2     Recife        60      4          3         accept      800

我想過濾數據集以便 select 僅具有最多 2 個房間和 2 個浴室的公寓。

你知道我該怎么做嗎?

嘗試

out = df.loc[(df.Rooms>=2) & (df.Bathrooms>=2)]

您可以使用query()方法:

out=test_gdata.query('Bathrooms<=2 and Rooms<=2')

您可以過濾 dataframe 上的值

import pandas as pd 

data = {
    'city': ['São Paulo', 'Rio', 'Recife'],
    'area(m2)': [90, 120, 60],
    'Rooms': [3, 2, 4],
    'Bathrooms': [2, 3, 3],
    'animal': ['accept', 'do not accept', 'accept'],
    'rent($)': [2000, 3000, 800]}

df = pd.DataFrame(
    data,
    columns=['city', 'area(m2)', 'Rooms', 'Bathrooms', 'animal', 'rent($)'])

df_filtered = df[(df['Rooms'] <= 2) & (df['Bathrooms'] <= 2)]

print(df_filtered)

退貨

        city  area(m2)  Rooms  Bathrooms         animal  rent($)
0  São Paulo        90      3          2         accept     2000
1        Rio       120      2          3  do not accept     3000
2     Recife        60      4          3         accept      800

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM