python - Converting space separated integers to lists in a dataframe -


i have dataframe looks :

[['label1', 'label2']   ['1 2 3', '1 2 3']   ['4 5 6', '4 5 7']] 

so each column considered strings. apply l2 distance between each element of 2 labels need convert columns list of float.

is there "clean" way that, better double loop [float(x) x in element.split()] ? (my dataframe huge quite optimized)

apply str.join, join both columns, split again using str.split. finally, convert float using df.astype.

df    label1 label2 0  1 2 3  1 2 3 1  4 5 6  4 5 7  df = df.apply(' '.join).str.split(expand=true).astype(float).t df     label1  label2 0     1.0     1.0 1     2.0     2.0 2     3.0     3.0 3     4.0     4.0 4     5.0     5.0 5     6.0     7.0 

based on comments, use applymap (slow)

from functools import partial f = partial(lambda x: [float(y) y in  x.split()])  df = df.applymap(f) df              label1           label2 0  [1.0, 2.0, 3.0]  [1.0, 2.0, 3.0] 1  [4.0, 5.0, 6.0]  [4.0, 5.0, 7.0] 

to apply columns, use

c = ['label1', 'label2'] # add other columns, if want  df[c] = df[c].applymap(f) 

note retaining columns lists, lose out on of pandas vectorisation benefits.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -