Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import os
  2. from collections import defaultdict
  3. from operator import itemgetter
  4. from itertools import groupby
  5.  
  6. from tabulate import tabulate
  7.  
  8.  
  9. class DataFrame:
  10. def __init__(self, headers, rows):
  11. self._headers = headers
  12. self._rows = rows
  13.  
  14. @property
  15. def headers(self):
  16. return self._headers
  17.  
  18. @headers.setter
  19. def headers(self, new_headers):
  20. assert len(self._headers) == len(new_headers)
  21. pass
  22.  
  23.  
  24. @property
  25. def rows(self):
  26. return self._rows
  27.  
  28. def group_by(self, by):
  29. pass
  30.  
  31. def merge(self, df, by):
  32. pass
  33.  
  34. def __getitem__(self, name):
  35. pass
  36.  
  37. def __setitem__(self, name):
  38. pass
  39.  
  40. def __str__(self):
  41. return tabulate(self.rows, headers=self.headers)
  42.  
  43. @staticmethod
  44. def from_file(path):
  45. pass
  46.  
  47. def to_csv(path):
  48. pass
  49.  
  50.  
  51. class GrouppedDataFrame:
  52.  
  53. def __init__(self, by, headers, groups):
  54. self._by = by
  55. self._headers = headers
  56. self._groups = groups
  57.  
  58. def sum_by(self, by):
  59. pass
  60.  
  61. BASE_PATH = '/home/olya/Downloads'
  62. PATH = os.path.join(BASE_PATH, 'spb_cameras.csv')
  63. POPULATION_PATH = os.path.join(BASE_PATH, 'spb_population_per_district.csv')
  64. CAMERAS_PATH = os.path.join(BASE_PATH, 'cameras_per_district.csv')
  65.  
  66. # ===================================================
  67. df = DataFrame.from_file(PATH)
  68. amount_df = df.group_by('district').sum_by('amount')
  69. amount_df.headers = ['Район', 'Число Камер']
  70. amount_df.to_csv('cameras_per_district.csv')
  71. print(amount_df)
  72.  
  73. # ===================================================
  74. amount_df = DataFrame.from_file(CAMERAS_PATH)
  75. pop_df = DataFrame.from_file(POPULATION_PATH)
  76. full_df = amount_df.merge(pop_df, by='Район')
  77. print(full_df)
  78.  
  79.  
  80. full_df['Плотность'] = # ...
  81. full_df.to_csv('exam_done.csv')
  82. print(full_df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement