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]
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
Asked: 2022-05-01 11:00:00 +0000
Seen: 14 times
Last updated: Mar 15 '22
What is the method for concealing a Styler DataFrame column in Streamlit?
How do you update a dataframe within a for loop in R after passing a list?
How can the list within a dataframe be transformed so that it becomes a binary data type?
How can I include the hours component to a DateTime column using PowerQuery?