santoku
santoku

Reputation: 3447

Convert List object in a pandas dataframe to numpy array

One of my pandas dataframe column is a list object of m columns, each row looks like this 'List(0.42, 0.24, 0.78,...)' with a list of n elements wrapped by quote marks. Dtype for this column is Object.

I need to convert this column into a m X n np array. So far I tried applying np.fromstring(col content) but it's mostly returning 'ValueError: string size must be a multiple of element size'. It did work for the first row though.

How to appropriately convert this List object column to an array?

Upvotes: 0

Views: 1206

Answers (1)

BENY
BENY

Reputation: 323376

We need trim your string , then split

np.array(s.str.strip('List').str.strip('(|)').str.split(', ').tolist())
Out[11]: 
array([['0.42', '0,24', '0.78,...'],
       ['0.42', '0,24', '0.78,...']], dtype='<U8')

Updated

s.str.strip('List').str.strip('(|)').str.split(',',expand=True).apply(lambda x : x.str.strip()).values
Out[18]: 
array([['0.42', '0', '24', '0.78', '...'],
       ['0.42', '0', '24', '0.78', '...']], dtype=object)

Upvotes: 1

Related Questions