Home > Mobile >  How to split a series with array to multiple series?
How to split a series with array to multiple series?

Time:12-12

One column of my dataset contains numpy arrays as elements. I want to split it to multiple columns each one with a value of the array. Data now are like:

        column
0   np.array([1,2,3,4])
1   np.array([5,6,7,8])

I want to convert it into:

   col1 col2 col3 col4
0    1    2    3    4
1    5    6    7    8

CodePudding user response:

As an alternative:

df = pd.DataFrame(data={'col':[np.array([1,2,3,4]),np.array([5,6,7,8])]})

new_df = pd.DataFrame(df.col.tolist(), index= df.index) #explode column to new dataframe and get index from old df.
new_df.columns = ["col_{}".format(i) for i in range(1,len(new_df.columns)   1)]

'''
   col_1  col_2  col_3  col_4
0      1      2      3      4
1      5      6      7      8
'''

CodePudding user response:

I hope I've understood your question well. You can leverage the result_type="expand" of the .apply method:

df = df.apply(
    lambda x: {f"col{k}": vv for v in x for k, vv in enumerate(v, 1)},
    result_type="expand",
    axis=1,
)
print(df)

Prints:

   col1  col2  col3  col4
0     1     2     3     4
1     5     6     7     8

CodePudding user response:

Another possible solution, based on pandas.DataFrame.from_records:

out = pd.DataFrame.from_records(
    df['col'], columns=[f'col{i 1}' for i in range(len(df.loc[0, 'col']))])

Output:

   col1  col2  col3  col4
0     1     2     3     4
1     5     6     7     8
  • Related