In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use pandas to achieve dichotomy search in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Excel solution
First of all, we need to know how much each person has been taken before picking up the goods:
Through a simple fixed address, you can directly use the sum function
The record in the first row is special. It is handled simply and manually filled with 0 (complex processing will make the sum formula a little more complicated)
By the same token, process the batch table:
Now you can use vlookup directly:
Note that the last parameter is 1, fuzzy lookup
The premise here is that the numerical column in the table on the right must be in ascending order, otherwise the result may be unexpected.
Pandas solution
Basically, every line of code in pandas corresponds to an operation in Excel:
Lines 1, 2: load data, not much to say
Rows 4, 5: sort 2 tables. This is actually a crucial step. These two operations are omitted from the above Excel operation.
Lines 7, 8: corresponding to the operation of finding the "cumulative column" in Excel
The line 10:pd.cut is equivalent to the vlookup of fuzzy lookup. But this method is more troublesome than vlookup, the number of partition points of each interval must be 1 more than that of the interval (this is in line with data segmentation logic, but it is not convenient for data table operation)
So in the parameter bins, we deliberately add a relatively large number of 1000.
The parameter right=False is to let the boundary value be treated as the left boundary. For example, the quantity 50 belongs to batch 1, not batch 2.
In view of the shortcomings of pd.cut mentioned above, we can customize a function to simplify the operation:
Add the sum of the data source (parameter x) to the segmentation point (parameter bins)
To make its behavior consistent with Excel vlookup by default, set the default value of the parameter right to False
Now the call is much clearer:
After reading the above, do you have any further understanding of how to use pandas to achieve dichotomy search in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.