Get item of list in dataframe column using loop

Viewed 225

I have dataframe df like this :

    date        item
    2019-03-29  [book,pencil]
    ...

I want to get every item in list using loop. this is what I've tried:

    for i in range(len(df)):
        for x in df['attributeName'][i]:
            print(x)

But i got every single character. The desired output is:

    book
    pencil

How do I solve this problem?

1 Answers
df2['item'].apply(pd.Series).unstack().reset_index(drop=True)

A better alternative.. A Common Pitfall: Exploding Columns of Lists

pd.DataFrame(df2['item'].tolist()).unstack().reset_index(drop=True)

Output

0      book
1    pencil
dtype: object
Related