Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ord() values from single-character columns in Pandas [duplicate]

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?

like image 755
Jivan Avatar asked Nov 20 '25 19:11

Jivan


1 Answers

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
>>> 
like image 146
U12-Forward Avatar answered Nov 23 '25 09:11

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!