How do I convert a Python class object that has fields that instantiate other classes to a DataFrame? I tried the following code below but it does not work.
I can get it to work when I take out self.address = Address() and self.agency_contact_info = ContactInfo()
class Address:
def __init__(self):
self.address_one = "address 1"
self.address_two = "P.O. BOX 1"
class ContactInfo:
def __init__(self):
self.person_name = "Me"
self.phone_number = "999-999-9999"
class AgencyRecord:
def __init__(self):
self.agency_code = "00"
self.agency_id = "000"
self.agency_name = "Some Agency"
self.address = Address()
self.agency_contact_info = ContactInfo()
def create_data():
data = {}
for i in range(0, 3):
alc = AgencyRecord()
data[i] = alc
column_list = [
'agency_code', 'agency_id', 'agency_name',
'address_one', 'address_two', 'person_name', 'phone_number'
]
spark.createDataFrame(
list(data.values()),
column_list
).createOrReplaceTempView("MyTempTable")