Home > Net >  Sort cell text Alphabetically
Sort cell text Alphabetically

Time:06-21

I would like to update a column in a pandas dataframe to rearrange the string in alphabetical order. For example if a have a column named "Number" that contained : [123, 321, 456, 654], I would like this to sort each string entry alphabetically and add this as a new column.

# List of Number 
numbers = [123, 321, 456, 654]
# Create a DataFrame object 
df = pd.DataFrame(numbers, columns =['Number'])

The output should be :

enter image description here

I am quite the pandas and python novice so a step by step breakdown and explanation of code would be very much appreciated

CodePudding user response:

You can use apply and a custom function:

df['Number2'] = df['Number'].apply(lambda x: ''.join(sorted([num for num in str(x)])))

print(df)

   Number Number2
0     123     123
1     321     123
2     456     456
3     654     456

Breakdown:

# Step 1: break the cells into a list of values
>>> df['Number'].apply(lambda x: [num for num in str(x)])

0    [1, 2, 3]
1    [3, 2, 1]
2    [4, 5, 6]
3    [6, 5, 4]

# Step 2: sort the values within a list
>>> df['Number'].apply(lambda x: sorted([num for num in str(x)]))

0    [1, 2, 3]
1    [1, 2, 3]
2    [4, 5, 6]
3    [4, 5, 6]

# Step 3: join the sorted values and convert to int (if necessary)
>>> df['Number'].apply(lambda x: ''.join(sorted([num for num in str(x)]))).astype(int)

0    123
1    123
2    456
3    456
Name: Number, dtype: int32

CodePudding user response:

You can try

df['Number2'] = df['Number'].astype(str).apply(sorted).str.join('')
print(df)

   Number Number2
0     123     123
1     321     123
2     456     456
3     654     456
  • Related