0

I have a df containing a column with strings separated by commas which i try to sort alphabetically.

For a simple list like

data = ['B', 'C', 'A', 'D']

I would use something like

', '.join(sorted(data))

which works fine. However, for my df which Looks like

d = {'col1': [1, 2], 'col2': ['D, D, A, C', 'B, A, B, A']}
df = pd.DataFrame(data=d)

I am not able to sort row wise the col2 alphabetically. I tried so far

print ', '.join(sorted(df['col2']))

which returns a complete sorting but not row wise. My expected result is:

res = {'col1': [1, 2], 'col2': ['A, C, D, D', 'A, A, B, B']}
result = pd.DataFrame(data=res)

Thanks, for your ideas!

2 Answers 2

1

You can use apply as follows:

df["col2"] = df.col2.apply(lambda x: ", ".join(sorted(x.split(", "))))

Explanation:

  • df.col2.apply(..) iterates through the rows of df.col2
  • For each row, it splits the string into a list using x.split(", ")
  • Each list is then sorted, using sorted(x.split(", "))
  • The sorted list is then concatenated to a string and assigned back to the row using

    ", ".join(sorted(x.split(", ")))
    

output:

   col1        col2
0     1  A, C, D, D
1     2  A, A, B, B
Sign up to request clarification or add additional context in comments.

Comments

0

another way would be,

Method -1

df['col2'] = [', '.join(sorted (val )) for val in df.col2.str.split(', ').values] # split the value by , sort the values and combined it with `, `
print(df)

Method -2:

df['col2'] = [', '.join(sorted(x.split(', '))) for x in df['col2']]
print(df) # for each value in cell split the value by `, ` sort the value and join by `, `

O/P:

   col1        col2
0     1  A, C, D, D
1     2  A, A, B, B

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.