/
create_dataframe.py
75 lines (59 loc) · 2.16 KB
/
create_dataframe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""This script makes a dataframe of latitudes and longitudes given a file path,
either by radio show or by radio station. """
import os
import json
import pandas as pd
def make_show_df(direct):
loc_df = {}
all_locs = []
#find location of show
with open(direct) as in_file:
show_data = json.load(in_file)
first_ep = show_data[0]
ep_loc = first_ep['ep_coords']
ep_lat = float(ep_loc[1])
ep_long = float(ep_loc[2])
loc_df['location'] = [first_ep['show_name'],ep_lat,ep_long]
#compile all locations
for episode in show_data:
all_locs += episode['locations']
#add locations to dictionary
for i in range(len(all_locs)):
loc = all_locs[i]
if type(loc) == list:
loc_df[str(i)] = [loc[0],loc[1],loc[2]]
#create pandas dataframe
df_for_map = pd.DataFrame.from_dict(loc_df,
orient='index',
columns=['title', 'latitude', 'longitude'])
return df_for_map
def make_station_df(direct):
loc_df = {}
all_locs = []
show_list = [d for d in os.listdir(direct) if os.path.isdir(direct + '/' + d)]
show_dir = [(direct + '/' + show + '/' + show + '.json') for show in show_list
if os.path.isfile(direct + '/' + show + '/' + show + '.json')]
#find location of station
with open(show_dir[0],'r') as in_file:
first_file = json.load(in_file)
first_ep = first_file[0]
ep_loc = first_ep['ep_coords']
ep_lat = float(ep_loc[1])
ep_long = float(ep_loc[2])
loc_df['location'] = [first_ep['station'],ep_lat,ep_long]
#compile locations
for file in show_dir:
with open(file, 'r') as in_file:
show_data = json.load(in_file)
for episode in show_data:
all_locs += episode['locations']
#add locations to dict
for i in range(len(all_locs)):
loc = all_locs[i]
if type(loc) == list:
loc_df[str(i)] = [loc[0],loc[1],loc[2]]
#create pandas dataframe
df_for_map = pd.DataFrame.from_dict(loc_df,
orient='index',
columns=['title', 'latitude', 'longitude'])
return df_for_map