3

With a dataframe like this:

>>> df = pd.DataFrame([
    ['a', 'b', 'c'],
    ['d', 'e', 'f'],
    ['x', 'y', 'z'],
])

We can get the ord() mapping for each character:

>>> ordinals = df.apply(lambda x: [ord(c) for c in x])

>>> ordinals
     0    1    2
0   97   98   99
1  100  101  102
2  120  121  122

Is it possible to get the same result in a vectorised manner, so that we avoid using apply on each row which is extremely slow with 1.5M+ rows?

Jivan
  • 21,522
  • 15
  • 80
  • 131

1 Answers1

1

Use applymap:

df.applymap(ord)

Ex:

>>> df.applymap(ord)
     0    1    2
0   97   98   99
1  100  101  102
2  120  121  122
>>> 

Or with np.vectorize (might be quicker, since it's numpy):

>>> df[:] = np.vectorize(ord)(df)
>>> df
     0    1    2
0   97   98   99
1  100  101  102
2  120  121  122
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114