2017-12-15 14:24:17 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-21 13:01:11 +01:00
|
|
|
import os;
|
|
|
|
os.chdir('J:\PycharmProjects\Python2017\labs06')
|
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib
|
|
|
|
matplotlib.style.use('ggplot')
|
|
|
|
plt.rcParams['figure.figsize'] = (15, 5)
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
def wczytaj_dane():
|
2018-01-21 13:01:11 +01:00
|
|
|
dane = pd.read_csv('mieszkania.csv', sep=',')
|
|
|
|
dane.rename(columns={'Unnamed: 11': 'Borough'}, inplace=True)
|
|
|
|
#type(dane)
|
|
|
|
#print(dane.head())
|
|
|
|
return(dane)
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def most_common_room_number(dane):
|
2018-01-21 13:01:11 +01:00
|
|
|
m = dane.groupby(['Rooms']).count()
|
|
|
|
return(m['Id'].idxmax())
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2018-01-21 13:03:13 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def cheapest_flats(dane, n):
|
2018-01-21 13:01:11 +01:00
|
|
|
dane2 = dane.sort_values(by=['Expected'])
|
|
|
|
return(dane2.head(n))
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def find_borough(desc):
|
|
|
|
dzielnice = ['Stare Miasto',
|
|
|
|
'Wilda',
|
|
|
|
'Jeżyce',
|
|
|
|
'Rataje',
|
|
|
|
'Piątkowo',
|
|
|
|
'Winogrady',
|
|
|
|
'Miłostowo',
|
|
|
|
'Dębiec']
|
2018-01-21 13:01:11 +01:00
|
|
|
tekst = desc.split(" ")
|
|
|
|
for i in range(len(tekst)):
|
|
|
|
for j in range(len(dzielnice)):
|
|
|
|
if tekst[i] == dzielnice[j]:
|
|
|
|
return(dzielnice[j])
|
|
|
|
else:
|
|
|
|
j = j + 1
|
|
|
|
i = i + 1
|
|
|
|
return('Inne')
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def add_borough(dane):
|
2018-01-21 13:01:11 +01:00
|
|
|
for i in range(5000):#len(dane.axes[0])):
|
|
|
|
dane['Borough'][i] = find_borough(dane['Location'][i])
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def write_plot(dane, filename):
|
2018-01-21 13:01:11 +01:00
|
|
|
my_plot = dane['Borough'].hist()
|
|
|
|
fig = my_plot.get_figure()
|
|
|
|
|
|
|
|
dir_name = 'J:/PycharmProjects/Python2017/labs06/'
|
|
|
|
filename_suffix = 'pdf'
|
|
|
|
fig.savefig(os.path.join(dir_name, filename + "." + filename_suffix))
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def mean_price(dane, room_number):
|
2018-01-21 13:01:11 +01:00
|
|
|
m = dane.groupby(['Rooms']).mean()
|
|
|
|
return(m['Expected'][room_number])
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def find_13(dane):
|
2018-01-21 13:01:11 +01:00
|
|
|
df_filtered = dane[dane['Floor'] == 13]
|
|
|
|
return(df_filtered['Borough'])
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def find_best_flats(dane):
|
2018-01-21 13:01:11 +01:00
|
|
|
df_filtered = dane[dane['Floor'] == 1]
|
|
|
|
df_filtered2=df_filtered[df_filtered['Borough']=='Winogrady']
|
|
|
|
df_filtered3 = df_filtered2[df_filtered2['Rooms'] == 3]
|
2017-12-15 14:24:17 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-21 13:01:11 +01:00
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
def main():
|
|
|
|
dane = wczytaj_dane()
|
|
|
|
print(dane[:5])
|
|
|
|
|
|
|
|
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
|
|
|
|
.format(most_common_room_number(dane)))
|
|
|
|
|
|
|
|
print("{} to najłądniejsza dzielnica w Poznaniu."
|
2018-01-21 13:01:11 +01:00
|
|
|
.format(find_borough("Grunwald i Jeżyce")))
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
print("Średnia cena mieszkania 3-pokojowego, to: {}"
|
|
|
|
.format(mean_price(dane, 3)))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|