Home > Enterprise >  How to concatenating the elements of an array with different length?
How to concatenating the elements of an array with different length?

Time:05-31

I have a list1 = [1,2,3,4,5,6,7,8,9,0]. I want to take out one element "4", then split the remaining list with np.array_split(list1,5), I will get [array([1, 2]), array([5, 6]), array([7, 8]), array([ 9, 10]), array([11])] as result. When I try to convert it into an pandas Data Frame, the out put result would be as:

index 0 1
0 1 2.0
1 5 6.0
2 7 8.0
3 9 10.0
4 11 NaN

But I want to get the result as just one column data frame without NaN value at the end.

Any suggestion with this matter would be appreciated.

CodePudding user response:

Put your array into a dict and create your dataframe from that:

list1 = [1,2,3,4,5,77,8,9,0]
x = np.array_split(list1, 5)

df = pd.DataFrame({'column': x})

Output:

>>> df
    column
0   [1, 2]
1   [3, 4]
2  [5, 77]
3   [8, 9]
4      [0]
  • Related