I have this DataFrame:
C1 C2
A 2:3:1:7
B 2:1:4:3
C 2:1:1:1
I need to sort the integers in C2
, keeping the colons.
The output should look like this:
C1 C2
A 1:2:3:7
B 1:2:3:4
C 1:1:1:2
The example above is for understanding, this is the output I have so far:
{'_c1': {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}, '_c2': {0: '2:3:7:9:1:8:6:1', 1: '5:1:3:9:4:6:8', 2: '6:7:5:0:9', 3: '3:1:5:5:2:7', 4: '1:2:8:3:8:9:7:3:4:6:5:5:1:5'}}
CodePudding user response:
df['C2'] = df['C2'].str.split(':').apply(lambda x: x.sort() or x).str.join(':')
Output:
>>> df
C1 C2
0 A 1:2:3:7
1 B 1:2:3:4
2 C 1:1:1:2
CodePudding user response:
No NaN's, you can use:
df['C2_new'] = [':'.join(sorted(x.split(':'))) for x in df['C2']]
Output:
C1 C2 C2_new
0 A 2:3:1:7 1:2:3:7
1 B 2:1:4:3 1:2:3:4
2 C 2:1:1:1 1:1:1:2