0

I have a series like below,

0    [1, 12, 23]
1    [2, 23, 54]
2        [3, 23]
3            [4]
dtype: object

I want to convert this single series in to dataframe based on the values in list(explode).

Expected Output:

    0   1   2
0   1   12  23
1   2   23  54
2   3   23  None
3   4   None    None

I tried,

s=pd.Series([[1,12,23],[2,23,54],[3,23],[4]])
s=s.astype(str)
s.str.replace('\[|\]','').str.split(', ',expand=True)

My above code does the job though I'm looking for good way to solve this.

Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111

1 Answers1

3

Use the below:

m = pd.DataFrame(s.values.tolist(), index= s.index)
print(m)

   0     1     2
0  1  12.0  23.0
1  2  23.0  54.0
2  3  23.0   NaN
3  4   NaN   NaN
anky
  • 74,114
  • 11
  • 41
  • 70