I have to following question. Is there a way to build a DataFrame from a list of python Generator objects. I used list comprehension to create the list with data for the dataframe:
data_list.append([record.Timestamp,record.Value, record.Name, record.desc] for record in records)
I did it this way because normal list append in a for loop is taking like 20x times longer:
for record in records:
data_list.append(record.Timestamp,record.Value, record.Name, record.desc)
I tried to create the dataframe but it doesn't work:
This:
dataframe = pd.DataFrame(data_list, columns=['timestamp', 'value', 'name', 'desc'])
Throws exception:
ValueError: 4 columns passed, passed data had 142538 columns.
I also tried to use itertools like this:
dataframe = pd.DataFrame(data=([list(elem) for elem in itt.chain.from_iterable(data_list)]), columns=['timestamp', 'value', 'name', 'desc'])
This results as a empty DataFrame:
Empty DataFrame\nColumns: [timestamp, value, name, desc]\nIndex: []
data_list looks like this:
[<generator object St...51DB0>, <generator object St...56EB8>,<generator object St...51F10>, <generator object St...51F68>]
Code for generating the list looks like this:
for events in events_list:
for record in events:
data_list.append([record.Timestamp,record.Value, record.Name, record.desc] for record in records)
This is required because of events list data structure.
Is there a way for me to create a dataframe out of list of Generators? If there is, is it going to be time efficient? What I mean is that I save a lot of time with replacing normal for loop with list comprehension, however if the creation of dataframe takes more time, this action will be pointless.