Home > front end >  pandas sort alphabetically for every row based on column content
pandas sort alphabetically for every row based on column content

Time:07-11

I have a dataframe that looks like this:

Col1 Col2
Bonnie Anna
Connor Ethan
Sophia Daniel

And I want to sort its content alphabetically so that the final result is:

Col1 Col2
Anna Bonnie
Connor Ethan
Daniel Sophia

I want each pair to be ordered alphabetically. As they are in different columns, I don't know how to sort them directly with sort_values method. Thanks!

CodePudding user response:

data = df.to_numpy()
data.sort(axis=1)
pd.DataFrame(data, columns=df.columns)

CodePudding user response:

You can sort each row with DataFrame.apply

out = (df.apply(lambda row: sorted(row), axis=1, result_type='expand')
       .set_axis(df.columns, axis=1))
print(out)

     Col1    Col2
0    Anna  Bonnie
1  Connor   Ethan
2  Daniel  Sophia

CodePudding user response:

You need to make a list for each row, then sort that list:

col_names = dta.columns # save the column names
dta = dta.apply(lambda x:list(x), axis=1) # convert each row to a list
dta = dta.apply(lambda x: sorted(x)) # sorting the row lists
dta = dta.to_list() # convert the Series to a list
dta = pd.DataFrame(dta) # recreate the data frame
dta.columns = col_names # replace the name of columns

Also you can do the whole thing in one line:

dta = pd.DataFrame(dta.apply(lambda x: sorted(list(x)), axis=1).to_list())
dta.columns = col_names

result:

Col1 Col2
0 Anna Bonnie
1 Connor Ethan
2 Daniel Sophia

CodePudding user response:

You can try this:

df[['col1', 'col2']] = pd.DataFrame(
    zip(*df[['col1', 'col2']].apply(sorted, axis=1).values)
).T
  • Related