Change the order of columns in Pandas dataframe

Now, in this post, we are going to see different kinds of column reorder methods. There are most common methods list are as follows:
  1. Using column selection [ ]
  2. Using the reindex method
  3. Using column selection through column index
  4. Columns reorder using the .iloc
  5. Columns reorder using the .loc
  6. Reorder Columns using Pandas .insert()
  7. Reorder the column of dataframe using ascending order
  8. Reorder the column of dataframe using a descending order

Method 1: Using column selection [ ]

The first method we will discuss is to reorder the names of the columns of the pandas. DataFrame is a selection [ ]. This is the very easiest method to reorder the columns.

In Cell [55]: We will create a dictionary with the key values name, age, city, and marks.

In cell [56]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [57]: We are displaying our newly created dummy dataframe.

In cell [58]: Now, we are reordering the columns using the selection [ ]. In that, we re-arrange the names of the columns as per our requirements. From the results, we can see that our original dataframe columns were in the order of (name, age, city, marks), but after changing their order, the orders of the dataframe columns in the form of (name, city, city, marks, age).

Method 2: Using the reindex method

The next method which we are going to use is the reindex. This is the most common way to use re-order the columns of a dataframe. As with the selection method, this is also a very simple method. We can access this method using the df. reindex(columns =[ names of the columns]) as shown below:

In Cell [59]: We will create a dictionary with the key values name, age, city, and marks.

In cell [60]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [61]: We are displaying our newly created dummy dataframe.

In cell [62]: Now, we are using the reindex method, which is a very simple method. In this, we just call the method df. reindex and set the name of the columns according to our requirements. And from the result, we can see that the order of the column changed from the original dataframe.

Method 3 : Using column selection through column index

The next method which we are going to discuss is the column index. The column index is also a very famous method and easy to use. This method is very similar to the reindex method. In the reindex method, we supply the re-order names of the columns, but here we supply the re-order names of the columns in the form of their index value, not the actual name of the columns as shown below:

In Cell [63]: We will create a dictionary with the key values name, age, city, and marks.

In cell [64]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [65]: We are displaying our newly created dummy dataframe.

In cell [66]: We call the method df. columns, and we passed their columns index value according to our re-order requirements. We print the newly created dataframe (df_re), and from the results, we found that columns finally re-order.

Method 4: Columns reorder using the .iloc

Let’s first understand the loc and iloc method. We created a seried_df (Series) as shown below in the cell number [24]. We then print the series to see the index label along with the values. Now, at cell number [26], we are printing the series_df.loc[4], which gives the output c. We can see that the index label at 4 values is {c}. So we got the correct result.

Now at the cell number [27], we are printing series_df.iloc[4], and we got the result {e} which is not the index label. But this is the index location that counts from 0 to the end of the row. So, if we start to count from the first row then, we get {e} at index location 4. So, now we understand how this two similar loc and iloc works.

Now, we understand the loc and iloc method. So first, we are going to use the iloc method.

In Cell [67]: We will create a dictionary with the key values name, age, city, and marks.

In cell [68]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [69]: We are displaying our newly created dummy dataframe.

In cell [70]:  We passed the index values of the columns to the iloc and assigned the result to a new dataframe (df_new). From the results, we can see that the names of the columns are re-order.

Method 5: Columns reorder using the .loc

We have seen how to re-order the name of the columns using the iloc method. Now, we are going to implement the same using the loc method. We already know the loc method works with the index location. Here, we pass the name of the columns instead of the index value as shown below:

In Cell [71]: We will create a dictionary with the key values name, age, city, and marks.

In cell [72]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [73]: We are displaying our newly created dummy dataframe.

In cell [74]: In the above example, we passed the names of columns in a different order and the newly generated dataframe; when printed, we got the results that showed the columns’ names are reordered.

Method 6: Reorder Columns using Pandas .insert()

The next method which we are going to discuss is the insert ( ) method. This method is not used that much. The reason behind its long process. In this method, first, we create a copy of a particular column which location we want to change and then delete that column from the dataframe and then set that column to a new location as shown below.

In Cell [75]: We will create a dictionary with the key values name, age, city, and marks.

In cell [76]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [77]: We are displaying our newly created dummy dataframe.

In cell [78]: We first created a copy of the marks column. Then we drop (delete) that column from the dataframe. Then we insert the column (marks) to a new location between the name and age.

Method 7: Reorder the column of dataframe using ascending order

This method is useful only when we want to arrange the columns in ascending order. This method also changes the order of the columns, so we also keep this method in our article.

In Cell [79]: We will create a dictionary with the key values name, age, city, and marks.

In cell [80]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [81]: We are displaying our newly created dummy dataframe.

In cell [82]: We first create a list of all the columns of a dataframe. Then we sort the dataframe by calling the method sort() to the ascending order and then newly list we assigned to a dataframe like a selection method and generate a new dataframe and print that dataframe.

Method 8: Reorder the column of dataframe using a descending order

This method is similar to the ascending method. The only difference is that when we call the sort ( ) method, we pass a parameter reverse=True which arranges the names of the columns to the descending order as shown below:

In Cell [84]: We will create a dictionary with the key values name, age, city, and marks.

In cell [85]:  We convert those dictionaries to a pandas dataframe as shown in the above.

In cell [86]: We are displaying our newly created dummy dataframe.

In cell [87]: We call the sort ( ) method and pass a parameter reverse=True.

Conclusion

In this post, we studied the different kinds of pandas column reorder methods. We have also seen very easy methods like selection, reindex and column index methods, and .loc and .iloc. We have also seen at the end about ascending and descending methods. We did not include any custom methods for the columns reorder because any end-user defines custom methods. We tried our best to include all important methods which will be helpful in your projects.

So that’s all about the Pandas columns reorder.



from Linux Hint https://ift.tt/3xlG8fh

Post a Comment

0 Comments