Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

add numpy array to pandas dataframe


import numpy as np
import pandas as pd
import scipy.sparse as sparse

df = pd.DataFrame(np.arange(1,10).reshape(3,3))
arr = sparse.coo_matrix(([1,1,1], ([0,1,2], [1,2,0])), shape=(3,3))
df['newcol'] = arr.toarray().tolist()
print(df)

Source by nullorempty.org #
 
PREVIOUS NEXT
Tagged: #add #numpy #array #pandas #dataframe
ADD COMMENT
Topic
Name
5+8 =