Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numpy matrix to pandas dataframe or series by row

Tags:

python

pandas

I have a numpy matrix and I'd like to convert it to a pandas dataframe/series. An example:

m = np.array([[1, 2], [11, 22]])

which would result in

        a
0   [1,   2]
1   [11, 22]
like image 755
GuillaumeA Avatar asked Jan 27 '23 18:01

GuillaumeA


1 Answers

m = np.array([[1, 2], [11, 22]])
pd.DataFrame({'col':[z for z in m]})

    col
0   [1, 2]
1   [11, 22]

Or, as per @pault

pd.DataFrame({'col':list(m)})

    col
0   [1, 2]
1   [11, 22]
like image 64
rafaelc Avatar answered Jan 31 '23 23:01

rafaelc