Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of sorting, grouping groupby and summation of cumsum in pandas

Shulou Source: shulou.com Published: 2022-05-31 17:04:42 10月02日 Update

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']

Tags: Sorting maths Chinese English grouping grade method two Xiaofan knowledge subject Xiaobai Xiaohong Xiaolan Xiaoqing code content official situation score Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple macOS Shulou Information Shulou Technology