I have a Pandas Dataframe as per below, with an index and two columns. "Image_main" column consists of a list of urls.
What I want to do is to separate each of the items in the list of the column "image_main" into new columns in the same dataframe. The length of the list is different in each row. For example, list in row 1 has 4 urls, while row 3 has only 2 urls.
index image_main referenceID
0 ['https://x.com/1.jpg','https://x.com/2.jpg',... 3.297439e+10
1 ['https://y.com/1.jpg','https://y.com/2.jpg',... 4.000220e+12
2 ['https://z.com/1.jpg','https://z.com/2.jpg',... 4.000130e+12
3 ['https://v.com/1.jpg','https://v.com/2.jpg',... 3.296914e+10
4 ['https://a.com/1.jpg','https://a.com/2.jpg',... 4.000080e+12
So far, I have tried below based on the answers given to the following question: Pandas: split column of lists of unequal length into multiple columns . However, it does not seem to be working since I get the same result as I had before
df['image_main'] = pd.DataFrame(df['image_main'].values.tolist()).add_prefix('code_')
print(df)
image_main referenceID
0 ['https://x.com/1.jpg','https://x.com/2.jpg',... 3.297439e+10
1 ['https://y.com/1.jpg','https://y.com/2.jpg',... 4.000220e+12
2 ['https://z.com/1.jpg','https://z.com/2.jpg',... 4.000130e+12
3 ['https://v.com/1.jpg','https://v.com/2.jpg',... 3.296914e+10
4 ['https://a.com/1.jpg','https://a.com/2.jpg',... 4.000080e+12
How can I split each of the items in the column image_main into new separate columns in the same dataframe?
The desired result would something similar to below:
image_main referenceID. image_1. image 2 ....
0 ...,... 3.297439e+10. 'https://x.com/1.jpg' 'https://x.com/2.jpg'
1 ...,... 3.297439e+10. 'https://y.com/1.jpg' 'https://y.com/2.jpg'
2 ...,... 3.297439e+10. 'https://z.com/1.jpg' 'https://z.com/2.jpg'
3 ...,... 3.297439e+10. 'https://v.com/1.jpg' 'https://v.com/2.jpg'
4 ...,... 3.297439e+10. 'https://a.com/1.jpg' 'https://a.com/2.jpg'