In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to sort in python according to the specified string sequence, the content is very detailed, interested friends can refer to it, I hope it can help you.
Raw Data Preview
It is evident from the table below that all the regions are arranged in disorder. This friend just wants to arrange the regions in the order of "Shenzhen, Guangzhou, Huizhou and Dongguan".
Some people may think that such a simple problem can not be done directly with Excel. That's true, but what if there are multiple such forms? And it's repeated every day. Do you still think manual operation is more convenient?
3. solve problems
To be honest, this is the first time I have encountered this problem in Python. I only remember that I completed this requirement in Excel at that time. How to deal with it in Python? I really wasn't thinking. So this knowledge point is new to me, and I think it's probably new to most of you.
import pandas as pd
#Customize a sequence
x = ['Shenzhen',' Guangzhou','Huizhou',' Dongguan']
#Read the data
df = pd.read_excel("workbook.xlsx")
#Set Region column to category data type
df["Region"] = df["Region"]. atype ("category")
# reorder_categories means to reorder the categories specified in the x sequence
# inplace=True to reorder existing categories
df["Region"].cat.reorder_categories(x, inplace=True)
df.sort_values("Region", inplace=True)
df
The results were as follows:
How to sort according to the specified string sequence in python is shared here. I hope the above content can be of some help to everyone and can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.