Home > OS >  Class list covert into dataframe by groups
Class list covert into dataframe by groups

Time:06-05

I have one database the type is <class 'list'>: [a1, a2, a3, a4, a5, b1, b2, b3, b4, b5, c1, c2, c3, c4, c5,....]

Hope the result to convert into a dataframe: (each 5 items in one group)

enter image description here

Could someone help me and the method hope to learn from you?

CodePudding user response:

Try with numpy reshape, l is your list here

import numpy as np 

out = pd.DataFrame(np.array(l).reshape((-1,5)))

CodePudding user response:

You can use numpy.reshape and then create a dataframe:

df = pd.DataFrame(np.reshape(l, (-1, 5)))

Output:

>>> df
    0   1   2   3   4
0  a1  a2  a3  a4  a5
1  b1  b2  b3  b4  b5
2  c1  c2  c3  c4  c5

>>> df[2]
0    a3
1    b3
2    c3
Name: 2, dtype: object
  • Related