Say I have a column which has values like:
JavaScript
x
6
1
**Input**
2
3
A12:67, A18:24, B11:89
4
A12:78, A19:100
5
B12:100, A20:19, A01:91
6
I would like the piece of code that I am working on to return:
JavaScript
1
6
1
**Expected Output**
2
3
B11:89, A12:67, A18:24
4
A19:100, A12:78
5
B12:100, A01:91, A20:19
6
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.
JavaScript
1
4
1
def sortitem(df):
2
sorteddf = df.apply(lambda x :",".join(sorted(x.split(", "), key=lambda y: int(re.search(r"(d+)", y).group(1)), reverse=True)))
3
return sorteddf
4
The code outputs:
JavaScript
1
6
1
**Output received**
2
3
A18:24, A12:67, B11:89
4
A19:100, A12:78
5
A20:19, B12:100, A01:91
6
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
JavaScript
1
2
1
df['Input'].apply(lambda r: ', '.join(sorted(r.split(', '), key=lambda s: int(s.split(':')[-1]), reverse=True)))
2
Pandas test:
JavaScript
1
8
1
df = pd.DataFrame({'Input':['A12:67, A18:24, B11:89','A12:78, A19:100','B12:100, A20:19, A01:91']})
2
df['Input'] = df['Input'].apply(lambda r: ', '.join(sorted(r.split(', '), key=lambda s: int(s.split(':')[-1]), reverse=True)))
3
>>> df
4
Input
5
0 B11:89, A12:67, A18:24
6
1 A19:100, A12:78
7
2 B12:100, A01:91, A20:19
8
NOTES:
r.split(', ')
– splits each cell value with comma+spacesorted(..., 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 toint
usingint(s.split(':')[-1])
) in descending order (due toreverse=True
', '.join(...)
– combines back the split chunks