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 use Python to separate boxes for the same data

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use Python to achieve the same data sub-box, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is separate boxes?

To put it simply, it is to put different things into a specified container according to specific conditions, such as putting green fruit in a basket, red basket and so on. This basket is the box, and the fruit is the data. Color is the condition.

What kind of data should be divided into boxes?

The data is mainly divided into continuous variables and classified variables, and the sub-box operation is mainly aimed at continuous variables.

Why do you want to separate the data into boxes?

Stability, time complexity, looking comfortably, improving accuracy, etc.

Train of thought

Given that last is the first in the list (and stored in the temp list), compare the latter data with last from the second, if the same is stored in temp.

When different, switch the last to a different number (and store it in temp) and put the temp list into an empty list.

Type 1: number

Realize the effect

[1 recollection 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5] # transformed into [1, 1, 2], [3, 3], [4], [5, 5, 5, 5]

Code implementation

Box = [1Power1] temp = [box [0]] box_list = [temp] for an in box [1Ranger:]: if a = = last: temp.append (a) else: last = a temp = [a] box_list.append (temp) print (box_list) # [1meme 1], [2meme 2meme 2], [3je 3] [4, 4], [5, 5, 5, 5] # implement traversing data by each box list (instead of traversing all) for boxs in box_list: for i in boxs: print (I) Type 2: tuple

Realize the effect

Box = [('Xiao Hei','20 years of charge 'Sichuan'), ('Xiao Hei','21 years of charge', 'Beijing'), ('Zhang San','18 minutes of 'Shanghai'), ('Zhang San','22'of 'Shanghai'), ('Zhang San','30'of 'Beijing'), ('Li Si','10'of 'Guangzhou') # put tuples with the same name in a list [('Xiao Hei','20') 'Sichuan'), ('Xiao Hei','21', 'Beijing'), [('Zhang San','18', 'Shanghai'), ('Zhang San','22', 'Shanghai'), ('Zhang San','30', 'Beijing')], [('Li Si','10', 'Guangzhou')]

Code implementation

Box = [('Xiao Hei','20 years of heroin 'Sichuan'), ('Xiao Hei','21''Beijing'), ('Zhang San','18''Beijing'), ('Zhang San','22''Shanghai'), ('Zhang San','30''Beijing'), ('Li Si','10' 'Guangzhou')] last = box [0] [0] temp = [box [0]] box_list = [temp] for an in box [1 box:]: if a [0] = = last: temp.append (a) else: last = a [0] temp = [a] box_list.append (temp) print (box_list) # implementation traverses each box list Data (instead of traversing all) for boxs in box_list: for i in boxs: print (I [0]) # 0 name 1 take age, 3 take address attached: the data can be divided into boxes by using the cut method of Python. Import pandas as pd import numpy as np from pandas import Series,DataFrame# randomly generates a set of data score_list = np.random.randint (25 and 100) # randomly generates a minimum value of 25 20 data with a maximum of 100s # the interval of the sub-box bins = [0Permian 59pm 70m 80100] # the sub-box score_cat = pd.cut (score_list Bins) # count the number of different intervals pd.value_counts (score_cat) # generate an empty DataFramedf = DataFrame () df ['Score'] = score_listdf [' Name'] = [pd.util.testing.rands (5) for i in range (20)] # generate 20 names df ['Categories'] = pd.cut (df [' Score'], bins,labels = ['fail', 'average', 'excellent' (awesome]) # labels corresponds to all the contents of the article "how to use Python to separate boxes for the same data" above in bins Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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