Reputation: 567
Suppose I have the following dataframe
C1 C2
John 4 3
Bob 5 7
Mary 6 5
Carl 5 6
James 4 3
How can I order my dataframe to have:
Carl, Mary, Bob, John, James
in an efficient way?
It is an arbitrary ordering and I might have the names stored in a variable orderedNames
Upvotes: 1
Views: 283
Reputation: 294546
define your ordering
arbitrary_ordering = ['Carl', 'Mary', 'Bob', 'John', 'James']
option 1
loc
df.loc[arbitrary_ordering]
option 2
reindex
df.reindex(arbitrary_ordering)
option 3
reindex_axis
df.reindex_axis(arbitrary_ordering)
All yield
C1 C2
Carl 5 6
Mary 6 5
Bob 5 7
John 4 3
James 4 3
alternative 1
df.iloc[df.index.to_series().map(arbitrary_ordering.index)]
alternative 2
pd.Categorical
df.index = pd.Categorical(df.index, categories=arbitrary_ordering)
df.sort_index()
Time Test
Upvotes: 2
Reputation: 3777
You can order them arbirtrarly using loc
(suppose your data are in df
)
df.loc[['Carl', 'Mary', 'Bob', 'John', 'James'], :]
However, if you want to order them by subset of columns use sort_values
df.sort_values(by='C1')
Or even more advanced would be sorting by custom key function. See this question: pandas sort lambda function
Upvotes: 2