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!