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

77 lines
2.2 KiB
Python
Raw Permalink Normal View History

2017-12-15 14:24:17 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2018-01-20 19:02:56 +01:00
import pandas as pd
import matplotlib.pyplot as plt
2017-12-15 14:24:17 +01:00
2018-01-20 19:02:56 +01:00
def wczytaj_dane ( ):
return pd.read_csv ( 'mieszkania.csv' )
2017-12-15 14:24:17 +01:00
2018-01-20 19:02:56 +01:00
def most_common_room_number ( dane ):
return dane[ 'Rooms' ].value_counts ( )
2017-12-15 14:24:17 +01:00
2018-01-20 19:02:56 +01:00
def cheapest_flats ( dane , n ):
cheapest = dane[ [ 'Expected' , 'Location' ] ].sort_values ( by=[ "Expected" ] , ascending=False )
return cheapest[ :n ]
2017-12-15 14:24:17 +01:00
2018-01-20 19:02:56 +01:00
def find_borough ( desc ):
dzielnice = [ 'Stare Miasto' , 'Wilda' , 'Jeżyce' , 'Rataje' , 'Piątkowo' , 'Winogrady' , 'Miłostowo' , 'Dębiec' ]
2017-12-15 14:24:17 +01:00
2018-01-20 19:02:56 +01:00
descSplit = desc.split ( ' ' )
dzielnica = [ ds for ds in descSplit if ds in dzielnice ]
return dzielnica[ 0 ] if len ( dzielnica ) > 0 else 'Inne'
2017-12-15 14:24:17 +01:00
2018-01-20 19:02:56 +01:00
def add_borough ( dane ):
dane[ 'Borough' ] = dane.apply ( lambda row: find_borough ( row[ 'Location' ] ) , axis=1 )
return dane
2017-12-15 14:24:17 +01:00
2018-01-20 19:02:56 +01:00
def write_plot ( dane , filename ):
dane2 = add_borough ( dane )
do_plota = dane2[ 'Borough' ].value_counts ( )
do_plota.plot ( kind='bar' )
# plt.show()
plt.savefig ( filename )
def mean_price ( dane , room_number ):
pokoje = dane[ dane[ 'Rooms' ] == room_number ]
return pokoje[ 'Expected' ].mean ( )
def find_13 ( dane ):
dane2 = add_borough ( dane )
pietro = dane2[ dane2[ 'Floor' ] == 13 ]
return ' '.join ( set ( pietro[ 'Borough' ].tolist ( ) ) )
def find_best_flats ( dane ):
dane2 = add_borough ( dane )
isWinogrady = dane2[ 'Borough' ] == 'Winogrady'
isPierwsze = dane2[ 'Floor' ] == 1
isTrzypokojowe = dane2[ 'Rooms' ] == 3
best = dane2[ isWinogrady & isPierwsze & isTrzypokojowe ]
return best
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.".format ( find_borough ( "Grunwald i Jeżyce" ) ))
print("Średnia cena mieszkania 3-pokojowego, to: {:.2f}".format ( mean_price ( dane , 3 ) ))
print("Dzielnice z ofertami na 13 pietrze: {}".format ( find_13 ( dane ) ))
print("Najlepsze oferty: {}".format ( find_best_flats ( dane ) ))
2017-12-15 14:24:17 +01:00
if __name__ == "__main__":
2018-01-20 19:02:56 +01:00
main ( )