Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Pandas Dataframe sorting Operation in Python data Analysis

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail the "Python data analysis Pandas Dataframe sorting operation method", the content is detailed, the steps are clear, the details are handled properly, I hope this "Python data analysis Pandas Dataframe sorting operation method" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

1. Sorting of indexes

DataFrame provides the sort_index () method to sort the index. The axis parameter specifies whether to sort the row index or the column index. The default is 0, which means to sort the row index, and 1 means to sort the column index. The ascending parameter specifies ascending or descending order, defaults to True for ascending order, and sets to False for descending order.

The specific usage is as follows:

Sort the row index in descending order:

Sort_df = df.sort_index (ascending=False) sort_df

Sort the column index in ascending order:

Sort_df = df.sort_index (axis=1) sort_df

two。 Sorting of valu

DataFrame provides the sort_values () method to sort the values. Compared with the sort_index () method, it has one more by parameter, receives a string or list to specify the row or column name to be sorted, and the rest are basically the same. The specific usage is as follows:

Sort in ascending order by the value of age:

Sort_df = df.sort_values (by= "age") sort_df

Sort by the value of age in ascending order, and then by the value of gender in descending order:

Sort_df = df.sort_values (by= ["age", "gender"], ascending= [True, False]) sort_df

The output is as follows:

After sorting, if you want to adjust the row index, you can reset the row index in the following ways.

Frame.reset_index (drop=True)

Setting the parameter drop=True means to delete the original index. If you do not want to delete the original index, you can simply add another column of the index. You do not need to set it as follows:

Read here, this "Python data analysis Pandas Dataframe sorting operation method" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report