In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the sorting of pandas, grouping groupby and cumsum cumulative summation of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that after reading this pandas sorting, grouping groupby and cumsum cumulative summation method article will have a harvest, let's take a look at it.
Generate a column of sum _ age to accumulate age df ['sum_age'] = df [' age'] .cumsum () print (df)
Generate a column of sum _ age_new to accumulate age according to gender and is_good ['sum_age_new'] = df.groupby ([' gender','is_good']) ['age'] .cumsum () print (df)
Rank age according to gender df ['rank_g'] = df.groupby ([' gender']) ['age'] .rank () print (df)
The rank () here, that is, 'rank_g', is not arranged in the order of 1, 2, 3, 4,
According to the official documentation, this function calculates the numerical data level (1 to n) along an axis. By default, equal values are assigned the same level, which is the average of the levels of those values.
Example:
Import pandas as pdobj = pd.Series (print (obj.rank ())
The code sorts [7,-5, 7, 4, 2, 0, 4] from small to earth. obviously, it can be sorted into [- 5, 0, 2, 4, 4, 7, 7]. The number 7 has two positions, 6 and 7, so which level should it be sorted to? According to the official document, the average is (6 / 7) / 2 / 6. 5, so both 7's have a level of 6. 5. Similarly, both 4's have a level of (4 / 4) / 2 / 2 / 4. 5.
Output:
0 6.5
1 1.0
2 6.5
3 4.5
4 3.0
5 2.0
6 4.5
Dtype: float64
After sorting the data, grouping and cumulative summation # sort Start Time, Connection Type grouping, temp accumulative summation cumsumwsw_1 = wsw.sort_values (['Start Time']) wsw_1.loc [:,' Connection Number'] = wsw_1.groupby (['Connection Type']) [' temp'] .cumsum ()
If start time is not sorted here, Connection Number will not count the number number of drilling and tripping in chronological order.
Pandas grouping sorting function
In a class, students take exams in Chinese, math and English, with corresponding scores.
Now, if you want to list the top five of each subject class, you need to include the subject, name, grade, and ranking.
This is achieved by the following code:
Import pandas as pda= ['Xiaohong', 'Xiaogu', 'Xiaolan', 'Xiaobai', 'Xiaoqing', 'Xiao Zi', 'Xiao Fan', 'Xiao silly', 'Xiao Hong', 'Xiao Green', 'Xiao Blue', 'Xiao Bai', 'Xiao Qing', 'Xiao Li', 'Xiao Fan', 'Xiao silly', 'Xiao Hong', 'Xiao Green', 'Xiao Blue', 'Xiao Bai', 'Xiao Qing', 'Xiao Zi', 'Xiao Pink', 'Xiao silly'] b = ['Chinese' 'Chinese', 'Mathematics', 'Mathematics', 'English', 'English'] c = [97 len (a), len (b) Len (c) df=pd.DataFrame ({'name':a,'kemu':b,'score':c}) df2=df.sort_values ([' kemu','score','name'], ascending= [1,0kemu' 1]) df2 ['rn'] = df2.groupby ([' kemu']) .rank (method='first',ascending = 0) ['score'] df2 [df2 [' rn']
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.