0

I looked at one question about 'How to convert column with a list of values into rows in Pandas DataFrame' in this link: How to convert column with list of values into rows in Pandas DataFrame. As the above question, this converted 'df' to 'df1'. In my purpose, I would like to make the contrary process that converts the df1 to df. How could I make that? Thanks.

df = pd.DataFrame({ 'var1': [['a', 'b', 'c'], ['d', 'e',], [], np.nan], 'var2': [1, 2, 3, 4] })

df

enter image description here

df1 = df.explode('var1')

df1

enter image description here

I found a similar answer using group.by(), but I prefer to return as a list in list like:

enter image description here

Trung Luu
  • 45
  • 5
  • 1
    Can you specify the question properly, like what should your output exactly look like with help of dataframe. – Kedar U Shet Dec 16 '21 at 08:11
  • I found one answer here: https://stackoverflow.com/questions/22219004/how-to-group-dataframe-rows-into-list-in-pandas-groupby But I would like to return a 'df2' like a list in list that: var1 var2 ['a','b','c'] 1 ['d','e'] 2 [] 3 NaN 4 – Trung Luu Dec 16 '21 at 08:40

0 Answers0