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?
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
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With