In [1]:
import json
import pandas as pd
import requests
In [2]:
# with open('data/courses.csv', 'r') as courses:
# courses_df = pd.read_csv(courses)
# with open('data/schools.csv', 'r') as schools:
# schools_df = pd.read_csv(schools)
with open('data/schools.json', 'r') as schools_json:
schools_json_df = pd.read_json(schools_json)
In [5]:
schools = []
for index, row in schools_json_df.iterrows():
schools.append(row['schools'])
In [6]:
schools_df = pd.DataFrame(schools)
In [7]:
schools_df.describe()
Out[7]:
In [8]:
schools_df['locations'][1][0].get('courses')
Out[8]:
In [10]:
schools_df['contacts'][0]
Out[10]:
In [14]:
schools_df.head(1)['postal_address']
Out[14]:
In [5]:
courses_df = courses_df.fillna('')
In [6]:
school_data = []
for index, row in schools_df.fillna('').iterrows():
course = courses_df[courses_df['Key']==row['Name']]
courses_data = []
if len(course) > 0:
for index, val in course.iterrows():
course_data = {
'course_name': val['Course'].strip(),
'course_length': val['Course Lenght'],
'level_length': val['Length(Weeks)'],
'min_level_length': val['Min'],
'max_level_length': val['Max'],
'am_tuition_offshore': val[' AM Tuition fee (Offshore)'].strip(),
'am_tuition_onshore': val[' AM Tuition fee (Onshore)'].strip(),
'pm_tuition_offshore': val[' PM Tuition fee (Offshore)'].strip(),
'pm_tuition_onshore': val[' PM Tuition fee (Onshore)'],
'enrolment_fee': val[' Enrolment Fee'].replace('$','').strip(),
'material_fee': val[' Material Fee'].replace('$','').strip(),
'mat_week_frequency': val['Mat Week frecuency'],
'morning_timetable': val[' Morning Timetable'].strip(),
'afternoon_timetable': val['Afternoon Timetable'].strip(),
'night_timetable': val['Night Timetable'].strip()
}
courses_data.append(course_data)
school = {
'name': row['Name'],
'address': row['Address'],
'city': row['City'],
'amenities': row['Amenities'],
'surrounding': row['Surrounding'],
'logo_url': row['Logo URL'],
'latitude': row['Latitude'],
'longitude': row['Longitude'],
'courses': courses_data
}
school_data.append(school)
for data in school_data:
data['courses'] = [
{
'name': val['course_name'].strip(),
'course_length': val['course_length'],
'level_length': val['level_length'],
'max_level_length': val['max_level_length'],
'min_level_length': val['min_level_length'],
'am_tuition_offshore': 0 if val['am_tuition_offshore'].replace('$','').strip() == ''
else float(val['am_tuition_offshore'].replace('$','').strip()),
'am_tuition_onshore': 0 if val['am_tuition_onshore'].replace('$','').strip() == ''
else float(val['am_tuition_onshore'].replace('$','').strip()),
'pm_tuition_offshore': 0 if val['pm_tuition_offshore'].replace('$','').strip() == ''
else float(val['pm_tuition_offshore'].replace('$','').strip()),
'pm_tuition_onshore': 0 if val['pm_tuition_onshore'].replace('$','').strip() == ''
else float(val['pm_tuition_onshore'].replace('$','').strip()),
'enrolment_fee': 0 if val['enrolment_fee'].replace('$','').strip() == ''
else float(val['enrolment_fee'].replace('$','').strip()),
'material_fee': 0 if val['material_fee'].replace('$','').strip() == ''
else float(val['material_fee'].replace('$','').strip()),
'mat_week_frequency': 0 if val['mat_week_frequency'] == '' else int(val['mat_week_frequency']),
'morning_timetable': val['morning_timetable'],
'afternoon_timetable': val['afternoon_timetable'],
'night_timetable': val['night_timetable']
} for val in data['courses']
]
print(school_data[0])
In [13]:
api = "http://localhost:8080/api/v1/schools/bulk"
try:
response = requests.post(api, json=school_data)
print(response)
print(response.raise_for_status())
except Exception as e:
print(e)
In [7]:
api = "http://passporr.com/api/v1/schools/bulk"
try:
response = requests.post(api, json=school_data)
print(response)
print(response.raise_for_status())
except Exception as e:
print(e)
In [ ]: