Ask Your Question
0

What is the method for arranging values within a list column of a pandas dataframe in alphabetical order?

asked 2022-05-01 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-03-15 07:00:00 +0000

lakamha gravatar image

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]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-01 11:00:00 +0000

Seen: 14 times

Last updated: Mar 15 '22