Pandas GroupBy grouping takes the first few rows of each group record method

Source: Internet
Author: User
Tags pandas groupby
The following for everyone to share a pandas GroupBy group to take the first few lines of the record method, with a good reference value, I hope to be helpful to everyone. Come and see it together.

Directly on the example.

Import Pandas as PD df = PD. DataFrame ({' Class ': [' a ', ' a ', ' B ', ' B ', ' A ', ' a ', ' B ', ' C ', ' C '], ' score ': [3,5,6,7,8,9,10,11,14]})

Df:



class score
0 A 3
1 A 5
2 B 6
3 B 7
4 A 8
5 A 9
6 B 10
7 C 11
8 C 14

Df.sort_values ([' Class ', ' Score '],ascending=[1,0],inplace=true) grouped = Df.groupby ([' class ']). Head (2)

Grouped:



class score
5 A 9
4 A 8
6 B 10
3 B 7
8 C 14
7 C 11

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.