0

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

6
  • So what's the error? Please share it. Commented Apr 22, 2020 at 8:36
  • I have updated the error..it's an index error..it is basically not letting me assign a value from the list Commented Apr 22, 2020 at 8:38
  • The error is, while df is empty, you are trying to get an item from it with indexing. It's like trying to foo[5] while foo is an empty list. Commented Apr 22, 2020 at 8:39
  • Can you explain the lst a bit more? Like what is its pattern? Shouldn't it be lst=[1,"name1",10,2,"name2",20]? Commented Apr 22, 2020 at 8:42
  • basically len(lst) is not necessarily divisible by 3...so it can have the count which is not multiple of 3 Commented Apr 22, 2020 at 8:46

1 Answer 1

1

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

""""
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.