Create a list of dictionarys [{key:value, key:value}, {key:value, key:value}, {key:value, key:value}]
Add this straight as a dataframe. You can also control what is added this way by making a fucntion and passing data to it as the dictionary is built.
You can achieve this using itertools cycle if the rows are always in the correct order to the columns.
I assume that 3, name3, 30were incorrect and the list i think you should have should look like this.
cols = ['a','b','c']
rows = [1, "name1", 10, 2,"name2", 20, 3, "name3", 30]
And using the power of itertools
https://docs.python.org/3/library/itertools.html#itertools.cycle
cycle('abc') --> a b c a b c a b c a b c ...
I think this code can help you.
import itertools
def parse_data(data):
if data:
pass
#do something.
return data
cols = ['a','b','c']
rows = [1, "name1", 10, 2,"name2", 20, 3, "name3", 30]
d = [] # Temp list for dataframe to hold the dictionaries of data.
e = {} # Temp dict to fill rows & cols for each cycle.
for x, y in zip(itertools.cycle(cols), rows): # cycle through the cols but not the rows.
y = parse_data(y) # do any filtering or removals here.
if x == cols[0]: # the first col triggers the append and reset of the dictionary
e = {x:y} # re init the temp dictionary
d.append(e) # append to temp df list
else:
e.update({x:y}) # add other elements
print(e)
print(d)
df=pd.DataFrame(d) # create dataframe
print(df)
"""
a b c
1 name1 10
2 name2 20
3 name3 30
""""
dfis empty, you are trying to get an item from it with indexing. It's like trying tofoo[5]whilefoois an empty list.lsta bit more? Like what is its pattern? Shouldn't it belst=[1,"name1",10,2,"name2",20]?