Skip to content
Advertisement

Python regex function to arrange key:value in descending order. Wherein the key is alphanumeric and the value is digits

Say I have a column which has values like:

**Input**

A12:67, A18:24, B11:89
A12:78, A19:100
B12:100, A20:19, A01:91

I would like the piece of code that I am working on to return:

**Expected Output**

B11:89, A12:67, A18:24
A19:100, A12:78
B12:100, A01:91, A20:19

I’m trying to get the code to sort this key-value pair based on the value of the item in descending order This is the code I have written as of now, but it’s sorting based on the digits in the Key and not the value.

def sortitem(df):
    sorteddf = df.apply(lambda x :",".join(sorted(x.split(", "), key=lambda y: int(re.search(r"(d+)", y).group(1)), reverse=True)))
    return sorteddf

The code outputs:

**Output received**

A18:24, A12:67, B11:89
A19:100, A12:78
A20:19, B12:100, A01:91

Which is not the expected output. Please help with the corrections in the code required to achieve the desired output. Thank you.

Advertisement

Answer

You may use

df['Input'].apply(lambda r: ', '.join(sorted(r.split(', '), key=lambda s: int(s.split(':')[-1]), reverse=True)))

Pandas test:

df = pd.DataFrame({'Input':['A12:67, A18:24, B11:89','A12:78, A19:100','B12:100, A20:19, A01:91']})
df['Input'] = df['Input'].apply(lambda r: ', '.join(sorted(r.split(', '), key=lambda s: int(s.split(':')[-1]), reverse=True)))
>>> df
                     Input
0   B11:89, A12:67, A18:24
1          A19:100, A12:78
2  B12:100, A01:91, A20:19

NOTES:

  • r.split(', ') – splits each cell value with comma+space
  • sorted(..., key=lambda s: int(s.split(':')[-1]), reverse=True) – sorts the split chunks by the integer value that is after : (obtained by splitting the cell value with : and last item accessed with [-1] and then cast to int using int(s.split(':')[-1])) in descending order (due to reverse=True
  • ', '.join(...) – combines back the split chunks
Advertisement