Sometimes when working with DataFrames we might want to change or reverse the order of the column of the dataframe. In this article, let’s see how to reverse the order of the columns of a dataframe. This can be achieved in two ways –
Method 1: The sequence of columns appearing in the dataframe can be reversed by using the attribute.columns[::-1] on the corresponding dataframe. It accesses the columns from the end and outer dataframe[…] reindexes the dataframe using this new sequence provided.
Method 2: iloc indexer can also be used to reverse the column order of the data frame, using the syntax iloc[:, ::-1] on the specified dataframe. The contents are not preserved in the original dataframe.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course