Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. import google_maps as g
  2. import pandas as pd
  3. import re
  4.  
  5. file = pd.ExcelFile('./polling_stations.xlsx')
  6. writer = pd.ExcelWriter('./polling_stations_cleaned.xlsx',engine='xlsxwriter')
  7.  
  8. sheet_name = file.sheet_names[0]
  9. sheet = file.parse(sheet_name)
  10.  
  11. sheet = sheet.drop(columns="Address of Polling Station (Hindi)")
  12.  
  13. column_name = 'Locality'
  14. ward_lst = []
  15. ward_num = []
  16.  
  17. for i in range(len(sheet[column_name])):
  18. temp = sheet[column_name][i]
  19.  
  20. if re.match(r'PA|J',temp) is not None:
  21. ward_lst.append('PASCHIM VIHAR')
  22. ward_num.append(67)
  23.  
  24. elif re.match(r'RANI|MULTAN|NEW|RISHI|SHAKUR',temp) is not None:
  25. ward_lst.append('RANI BAGH')
  26. ward_num.append(66)
  27.  
  28. elif re.match(r'PE|SA',temp) is not None:
  29. ward_lst.append('SARASWATI VIHAR')
  30. ward_num.append(65)
  31.  
  32. ward_lat = []
  33. ward_long = []
  34.  
  35. column_name = 'Polling Area'
  36. booth_lat = []
  37. booth_long = []
  38. polling_addresses = []
  39.  
  40. count = 1
  41. i = 0
  42. while(i<len(sheet[column_name])):
  43. count = 0
  44. temp = sheet[column_name][i]
  45.  
  46. addr = re.split(r'[1-9] - ',temp)
  47. for item in addr:
  48. if item is not '':
  49. polling_addresses.append(item)
  50. count = count + 1
  51. i = i+count
  52. print(len(polling_addresses))
  53. for item in ward_lst:
  54. geo_data = g.get_geocoding(item)
  55. ward_lat.append(geo_data['latitude'])
  56. ward_long.append(geo_data['longitude'])
  57.  
  58. for addr in polling_addresses:
  59. geo_data = g.get_geocoding(addr)
  60. booth_lat.append(geo_data['latitude'])
  61. booth_long.append(geo_data['longitude'])
  62.  
  63. sheet['WardName'] = pd.Series(ward_lst)
  64. sheet['WardNumber'] = pd.Series(ward_num)
  65. sheet['WardLatitude'] = pd.Series(ward_lat)
  66. sheet['WardLongitude'] = pd.Series(ward_long)
  67. sheet['PollingBooth'] = pd.Series(polling_addresses)
  68. sheet['BoothLatitude'] = pd.Series(booth_lat)
  69. sheet['BoothLongitude'] = pd.Series(booth_long)
  70.  
  71. sheet.to_excel(writer, sheet_name=sheet_name)
  72. writer.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement