Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To arrange values within a list column of a pandas dataframe in alphabetical order, you can use the apply function with the sorted function as an argument. Here is an example code:

import pandas as pd

# create a dataframe
df = pd.DataFrame({'letters': [['d', 'c', 'a'], ['b', 'a', 'e'], ['f', 'e', 'g']]})

# sort the lists within the list column in alphabetical order
df['letters'] = df['letters'].apply(sorted)

# print the sorted dataframe
print(df)

Output:

        letters
0  [a, c, d]
1  [a, b, e]
2  [e, f, g]