Home > database >  Convert a pandas dataframe column with character to the alphabetic order
Convert a pandas dataframe column with character to the alphabetic order

Time:10-18

I'd like to know how to convert a column containing a single character on each row to the integer corresponding to its alphabetic order. From this:

enter image description here

to this:

enter image description here

Any hints on how to solve it?

Thanks! :)

CodePudding user response:

Here is an alternative solution using pandas.Series.map and enumerate.

import pandas as pd
import string


df = pd.DataFrame({"col": ["A", "B", "C"]})
#   col
# 0   A
# 1   B
# 2   C
df.col.map(
    {letter: index for index, letter in enumerate(string.ascii_uppercase, start=1)}
)
# 0    1
# 1    2
# 2    3
# Name: col, dtype: int64

CodePudding user response:

Use Series.rank:

df['col'] = df['col'].rank(method='dense').astype(int)

print (df)
   col
0    3
1    1
2    2
3    1
4    1

Or if all values are uppercase letter use:

df['col'] = df['col'].apply(ord) - 64

print (df)
   col
0    3
1    1
2    2
3    1
4    1
  • Related