Home > Mobile >  Pulling specific data with Pandas
Pulling specific data with Pandas

Time:12-12

I want to play with the data of house prices in Barcelona with Pandas, but there are amounts separated as square meters and monthly fees in the dataset, how can I get the prices of houses with only monthly amounts specifically?

Here is some data:

COLUMNS = ['Year', 'Trimester', 'District', 'Neighbourhood', 'Average _rent','Price']

  1. 2015,4,Sant Marti,Sant Marti de Provencals,average rent per surface (euro/m2),9.46
  2. 2015,4,Sant Marti,la Verneda i la Pau,average rent per surface (euro/m2),8.91
  3. 2016,1,Ciutat Vella,el Raval,average rent (euro/month),624.42
  4. 2016,1,Ciutat Vella,Gothic Quarter,average rent (euro/month),893.42
  5. 2016,1,Ciutat Vella,la Barceloneta,average rent (euro/month),638.75
  6. 2016,1,Eixample,Fort Pienc,average rent (euro/month),800.08
  7. 2016,1,Eixample,Sagrada Familia,average rent (euro/month),752.37

Sample Code:

import pandas as pd
import numpy as np

data = pd.read_csv('Barcelona_rent_price.csv')
print(data['Price']) # i just only want to get monthly prices
print(np.mean(data['Price']))

CodePudding user response:

Use pandas.Series.str.endswith function:

In [205]: df[df.Average_rent.str.endswith('/month)')].Price.mean()
Out[205]: 741.808

CodePudding user response:

Hope it works for your solution, we need to group by for year and month

import pandas as pd
df = pd.read_csv('./Barcelona_rent_price.csv')
df[df['Average _rent'] == 'average rent (euro/month)'].groupby(by=['Year', 'Average _rent']).agg({'Price': 'mean'})
  • Related