Reputation: 68
I have a Pandas DataFrame with several columns, one of which contains full names. I want to sort the DataFrame based on the last names.
I have a function get_last
that extracts the last name from a full name. I know that I can just create a new column that contains the last names, sort based on that column, and then drop it, something like
df['last_name'] = df['name'].apply(get_last)
df = df.sort_values('last_name').drop('last_name', axis=1)
but this doesn't seem like a very elegant or efficient solution.
If I was just sorting a list of full names by last name, I could just do
names = sorted(names, key=get_last)
but that doesn't work with a DataFrame.
Is there a better way to do this, or should I just stick with my current solution?
Upvotes: 0
Views: 38
Reputation: 323326
We can using argsort
df=df.iloc[df['name'].apply(get_last).argsort()]
Upvotes: 1