Sorting dataframe by specific column names in Pandas

Viewed 267

How to sort pandas's dataframe by specific column names? My dataframe columns look like this:

+-------+-------+-----+------+------+----------+
|movieId| title |drama|horror|action|  comedy  |
+-------+-------+-----+------+------+----------+
|                                              |
+-------+-------+-----+------+------+----------+

I would like to sort the dataframe only by columns = ['drama','horror','sci-fi','comedy']. So I get the following dataframe:

+-------+-------+------+------+------+----------+
|movieId| title |action|comedy|drama |  horror  |
+-------+-------+------+------+------+----------+
|                                               |
+-------+-------+------+------+------+----------+

I tried df = df.sort_index(axis=1) but it sorts all columns:

+-------+-------+------+------+-------+----------+
|action | comedy|drama |horror|movieId|  title   |
+-------+-------+------+------+-------+----------+
|                                                |
+-------+-------+------+------+-------+----------+
2 Answers

Sorting all columns after second column and add first 2 columns:

c = df.columns[:2].tolist() + sorted(df.columns[2:].tolist())
print (c)
['movieId', 'title', 'action', 'comedy', 'drama', 'horror']

Last change order of columns by this list:

df1 = df[c]

Another idea is use DataFrame.sort_index but only for all columns without first 2 selected by DataFrame.iloc:

df.iloc[:, 2:] = df.iloc[:, 2:].sort_index(axis=1)

You can explicitly rearrange columns like so

df[['movieId','title','drama','horror','sci-fi','comedy']]

If you have a lot of columns to sort alphabetically

df[np.concatenate([['movieId,title'],df.drop('movieId,title',axis=1).columns.sort_values()])]
Related