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

How to customize text sorting in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to customize text sorting in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Data introduction

There are three columns of data, sorted by "junior college", "undergraduate" and "graduate" for the field of "academic qualifications". For different academic qualifications, they are arranged according to the descending order of "basic salary".

Excel implements ① to select all data

② Click start-> sort and filter-> Custom sort ③ when the following interface appears, complete the operation ④ in the figure, when the following interface appears, complete the operation ⑤ in the figure, when the following interface appears, complete the operation ⑥ effect in the figure as follows: Python to implement import pandas as pd

# customize a sequence

X = ['postgraduate', 'undergraduate', 'junior college']

# read data

Df = pd.read_excel ("Test .xlsx")

# set the "academic qualifications" column to the category data type

Df ["academic qualifications"] = df ["academic qualifications"] .astype ("category")

# reorder_categories means to reorder the categories specified in the x sequence

# inplace=True means to reorder existing categories

Df ["academic qualifications"] .cat.reorder_categories (x, inplace=True)

# then use the way of multi-column sorting, arrange the "academic qualifications" column in the designated x order, and arrange the "basic salary" column in descending order

Df.sort_values (by= ["academic qualifications", "basic salary"], inplace=True,ascending= [True,False])

Df

Final result:

So much for the content of "how to customize text sorting in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report