TLanni
TLanni

Reputation: 340

populating empty dataframe from a list in python

I need to populate dataframe from the list.

lst=[1,"name1",10,2,"name2",2,"name2",20,3]
df=pd.DataFrame(columns=['a','b','c'])
j=0
for i in range(len(list(df.columns))-1):

   for t,v in enumerate(lst):
       col_index=j%3
       df.iloc[i,col_index]=lst[t]
       j=j+1

The above code is giving me an error.

i want df to be following

a  b     c
1  name1 10
2  name2  20
3  NaN    NaN

I have tried this but it is giving me a following error IndexError :Single positional indexer is out of bounds

Upvotes: 0

Views: 199

Answers (1)

johnashu
johnashu

Reputation: 2211

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

""""

Upvotes: 1

Related Questions