Get the App
SLTechnology News&Howtos  ›  Network Security  › 

How to sort the Pandas library DataFrame

Shulou Source: shulou.com Published: 2022-05-31 23:01:16 10月04日 Update

This article mainly introduces the Pandas library DataFrame how to sort, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Df1 is the test data of dataframe structure:

The df1 data is read from the test.xlsx document, and the sample code is as follows:

#-*-coding:utf-8-*-import tushare as tsimport pandas as pddf = pd.read_excel ('test.xlsx') df1 = df.head (10) # dataframe is sorted in ascending order. The default is ascending order # print df1.sort_index () # dataframe sorting in descending order # print df1.sort_index (ascending=False) # the first line is sorted in ascending order The default is ascending print df1.sort_index (axis=1) # the first line is sorted by descending order # print df1.sort_index (axis=1, ascending=False) # sort by the value of the column amount, by default from small to large # print df1.sort_values (by='amount') # sort by the value of the column amount, from large to small # print df1.sort_values (by='amount', ascending=False) # sort by multiple columns, and by the values of the amount and price columns From big to small # print df1.sort_values (by= ['amount',' price'], ascending=False) thank you for reading this article carefully. I hope the article "how to sort the DataFrame of Pandas Library" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Sort ascend article line data index from small to big small code value interest at the same time documents more friends knowledge examples structure tape industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Redmi Huawei Microsoft