1
0
forked from tdwojak/Python2017
Python2017/labs06/task02.py

91 lines
2.6 KiB
Python
Raw Normal View History

2017-12-15 14:24:17 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2018-01-19 21:55:49 +01:00
import pandas as pd
2017-12-15 14:24:17 +01:00
def wczytaj_dane():
2018-01-19 21:55:49 +01:00
mieszkania = pd.read_csv('C:\Users\mlyska\Documents\python2017\labs06\mieszkania.csv', sep = ',')
mieszkania.fillna('', inplace=True)
mieszkania['Description'] = mieszkania['Description'] + mieszkania['Unnamed: 7'] + mieszkania['Unnamed: 8'] + \
mieszkania['Unnamed: 9'] + mieszkania['Unnamed: 10'] + mieszkania['Unnamed: 11']
mieszkania.drop(['Unnamed: 7','Unnamed: 8','Unnamed: 9','Unnamed: 10','Unnamed: 11'], 1, inplace=True)
return mieszkania
2017-12-15 14:24:17 +01:00
def most_common_room_number(dane):
2018-01-19 21:55:49 +01:00
counter = dane['Rooms'].value_counts()
maxCount = counter.to_frame()
return maxCount.index.values[0]
2017-12-15 14:24:17 +01:00
def cheapest_flats(dane, n):
2018-01-19 21:55:49 +01:00
dane.sort_values('Expected')
return dane.head(n)
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-19 21:55:49 +01:00
for i in dzielnice:
if i in desc:
return i
break
if desc not in dzielnice:
return 'Inne'
2017-12-15 14:24:17 +01:00
def add_borough(dane):
2018-01-19 21:55:49 +01:00
values = []
for i in dane['Location']:
values.append(find_borough(i))
tempSeries = pd.Series(values)
dane['Borough'] = tempSeries.values
2017-12-15 14:24:17 +01:00
def write_plot(dane, filename):
2018-01-19 21:55:49 +01:00
add_borough(dane)
dataPlot = dane['Borough'].value_counts()
imgPlot = dataPlot.plot(kind='bar', alpha=0.5, title='Liczba ogloszen mieszkan z podzialem na dzielnice', fontsize=6, figsize=(8,7))
imgPlot.set_ylabel('Liczba ogloszen')
imgPlot.set_xlabel('Dzielnice')
filePlot = imgPlot.get_figure()
filePlot.savefig(filename)
2017-12-15 14:24:17 +01:00
def mean_price(dane, room_number):
2018-01-19 21:55:49 +01:00
x = dane.loc[dane['Rooms'] == room_number]
return x['Expected'].mean()
2017-12-15 14:24:17 +01:00
def find_13(dane):
2018-01-19 21:55:49 +01:00
x = dane.loc[dane['Floor'] == 13]
return list(set(x['Borough'].tolist()))
2017-12-15 14:24:17 +01:00
def find_best_flats(dane):
2018-01-19 21:55:49 +01:00
best = dane.loc[dane['Borough'] == 'Winogrady' & dane['Rooms'] == 3 & dane['Floor'] == 1]
return best
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-19 21:55:49 +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)))
2018-01-19 21:55:49 +01:00
write_plot(dane, 'ogloszenia.png')
2017-12-15 14:24:17 +01:00
if __name__ == "__main__":
main()