1
0
forked from tdwojak/Python2017

[homework lab6] ready for review

This commit is contained in:
s45155 2018-01-21 13:01:11 +01:00
parent ff3181b92e
commit 2077881ae8

View File

@ -1,15 +1,35 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
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)
def wczytaj_dane(): def wczytaj_dane():
dane = pd.read_csv('mieszkania.csv', sep=',')
dane.rename(columns={'Unnamed: 11': 'Borough'}, inplace=True)
#type(dane)
#print(dane.head())
return(dane)
pass pass
def most_common_room_number(dane): def most_common_room_number(dane):
m = dane.groupby(['Rooms']).count()
return(m['Id'].idxmax())
pass pass
def cheapest_flats(dane, n): def cheapest_flats(dane, n):
dane2 = dane.sort_values(by=['Expected'])
return(dane2.head(n))
pass pass
def find_borough(desc): def find_borough(desc):
dzielnice = ['Stare Miasto', dzielnice = ['Stare Miasto',
'Wilda', 'Wilda',
@ -19,24 +39,54 @@ def find_borough(desc):
'Winogrady', 'Winogrady',
'Miłostowo', 'Miłostowo',
'Dębiec'] 'Dębiec']
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')
pass pass
def add_borough(dane): def add_borough(dane):
for i in range(5000):#len(dane.axes[0])):
dane['Borough'][i] = find_borough(dane['Location'][i])
pass pass
def write_plot(dane, filename): def write_plot(dane, filename):
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))
pass pass
def mean_price(dane, room_number): def mean_price(dane, room_number):
m = dane.groupby(['Rooms']).mean()
return(m['Expected'][room_number])
pass pass
def find_13(dane): def find_13(dane):
df_filtered = dane[dane['Floor'] == 13]
return(df_filtered['Borough'])
pass pass
def find_best_flats(dane): def find_best_flats(dane):
df_filtered = dane[dane['Floor'] == 1]
df_filtered2=df_filtered[df_filtered['Borough']=='Winogrady']
df_filtered3 = df_filtered2[df_filtered2['Rooms'] == 3]
pass pass
def main(): def main():
dane = wczytaj_dane() dane = wczytaj_dane()
print(dane[:5]) print(dane[:5])
@ -45,7 +95,7 @@ def main():
.format(most_common_room_number(dane))) .format(most_common_room_number(dane)))
print("{} to najłądniejsza dzielnica w Poznaniu." print("{} to najłądniejsza dzielnica w Poznaniu."
.format(find_borough("Grunwald i Jeżyce")))) .format(find_borough("Grunwald i Jeżyce")))
print("Średnia cena mieszkania 3-pokojowego, to: {}" print("Średnia cena mieszkania 3-pokojowego, to: {}"
.format(mean_price(dane, 3))) .format(mean_price(dane, 3)))