DEV Community

Aisha Rajput
Aisha Rajput

Posted on • Updated on

Pandas reorder columns

pandas is a python library that helps to analyze data by using the data analysis algorithm of data science.
Pandas reorder columns by using multiple easy methods. Here we are going to reorder pandas columns categorically by index, by value, alphabetically, in reverse order, etc.
Here we first create a simple dataframe by using the pandas library.

#Simple dataframe
import pandas as pd
df = pd.DataFrame(
    {
        'Name'    : ['Brian', 'Morgan', 'Anderson', 'Clark', 'Junaid'],
        'Subject' : ['DSA', 'DS', 'OSDC', 'MAPD', 'CAAP'],
        'Language': ['Python', 'MATLAB','C', 'JAVA','Assembly'],
        'Marks'   :  [80, 70, 94, 78, 91]
    }
)
print(df),
Enter fullscreen mode Exit fullscreen mode

Output

  Name Subject  Language  Marks
0     Brian     DSA    Python     80
1    Morgan      DS    MATLAB     70
2  Anderson    OSDC         C     94
3     Clark    MAPD      JAVA     78
4    Junaid    CAAP  Assembly     91

Enter fullscreen mode Exit fullscreen mode

1. Pandas reorder columns based on list

Our first method pandas reorder columns based on the list. In this below method, set the column sequence as a list you want to display in the output. The output will print in the sequence that is mentioned as in the list such as print “language” column and so on as below.

#Pandas reorder columns based on list
df = df['Language', 'Marks', 'Subject', 'Name']
print(df)
Enter fullscreen mode Exit fullscreen mode

Output

   Language  Marks Subject      Name
0    Python     80     DSA     Brian
1    MATLAB     70      DS    Morgan
2         C     94    OSDC  Anderson
3      JAVA     78    MAPD     Clark
4  Assembly     91    CAAP    Junaid
Enter fullscreen mode Exit fullscreen mode

2. Pandas reorder columns by index

Use .reindex(columns) method to reorder columns in pandas. This method will help you reorder columns by using the index position name. By defining the sequence of columns inside the .reindex() function, you will get the output in that sequence.

#Pandas reorder columns by index
df = df.reindex(columns=['Subject', 'Language', 'Name', 'Marks'])
print(df)
Enter fullscreen mode Exit fullscreen mode

Output

    Subject  Language    Name  Marks
0     DSA    Python     Brian     80
1      DS    MATLAB    Morgan     70
2    OSDC         C  Anderson     94
3    MAPD      JAVA     Clark     78
4    CAAP  Assembly    Junaid     91
Enter fullscreen mode Exit fullscreen mode

3. pandas reorder columns alphabetically

Here we are going to use the sort method. Pandas reorder columns alphabetically by the sort index method that sorts all the columns in alphabetic sequence. Such as “Language, Marks, Name, Subject”

#pandas reorder columns alphabetically
sorted_column_df = df.sort_index(axis=1)
print(sorted_column_df)
Enter fullscreen mode Exit fullscreen mode

Output

   Language  Marks      Name Subject
0    Python     80     Brian     DSA
1    MATLAB     70    Morgan      DS
2         C     94  Anderson    OSDC
3      JAVA     78     Clark    MAPD
4  Assembly     91    Junaid    CAAP
Enter fullscreen mode Exit fullscreen mode

4. pandas reorder columns by value

this method helps to reorder columns in pandas by value. This method works internally on the column. Select the “Name” column and sorted that column values in ascending order.

#Pandas reorder columns by value
df = df.sort_values(by ='Marks' , ascending=True)
print(df)
Enter fullscreen mode Exit fullscreen mode

Output

       Name Subject  Language  Marks
1    Morgan      DS    MATLAB     70
3     Clark    MAPD      JAVA     78
0     Brian     DSA    Python     80
4    Junaid    CAAP  Assembly     91
2  Anderson    OSDC         C     94

Enter fullscreen mode Exit fullscreen mode

5. Pandas reorder columns in reverse

Pandas reorder columns in reverse order by using the built-in reverse () function.

# Pandas reorder columns in reverse
cols = list(df.columns)
cols.reverse()
df[cols]
Enter fullscreen mode Exit fullscreen mode

Output

     Marks  Language   Subject  Name
0   80   Python DSA    Brian
1   70   MATLAB DS     Morgan
2   94    C      OSDC      Anderson
3   78   JAVA      MAPD    Clark
4   91  Assembly   CAAP    Junaid

Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we have five different methods that help to reorder columns in pandas. We reorder columns on the based list, values, index, in reverse order, alphabetically, etc.

Top comments (0)