current position:Home>You can easily get started with Excel. Python data analysis package pandas (IV): any grouping score bar

You can easily get started with Excel. Python data analysis package pandas (IV): any grouping score bar

2021-08-23 04:48:01 Excel catalyst

Series articles :

> I often listen to others Python How powerful in the data field , As a result, I studied for a long time , Even data processing is a death of trouble . Only later , It's not Python Data processing is powerful , But he has a data analysis artifact —— pandas


In the last section, we introduced pandas How to make skills such as grade bar in , But that's according to Excel The solution is , However , In the face of complex and changeable needs , That way will be limited . This section will introduce a very flexible and clearer solution .

Case study

Continue to use the transcript data :

We want to list each student's transcript separately , That is, a row of records becomes a small table :

Ergodic thinking , But you don't need to traverse the code

In the previous section, we introduced how to generate empty rows in batch by using non-existent indexes . however , This is not universal and flexible enough . such as , We want to make a grade sheet by class , This method obviously can't do .

Use pandas The best part is this , You can write straightforward code based on ideas . Press " Class ", No, it is. " grouping " Do you . as follows :

  • - call df.groupby() , You can group data by any dimension
  • - pandas The grouping of is better than that of many mainstream databases Sql More flexible , He assigned each group to a subset of the group , So that we can operate flexibly , And you can also return multiple rows of records per group
  • - call apply , You can write the processing logic of each group in it
  • - apply The logic is very straightforward . Add a title at the top , Add a blank line at the end

The problem is coming. , You say this method is flexible , It can be grouped according to any dimension , But how does this approach get the initial requirements —— Each row has a small table ?

Corresponding to the initial demand , It's actually grouped by each line . that DataFrame What is different in each line ? you 're right , Is the row index (index). as follows :

More flexibility

This method can make flexible small tables , such as , By shift , Add a summary row at the end of each small table . The code is as follows :

  • - Add summary logic on the basis of the previous
  • - adopt df.append , You can easily add a summary row to DataFrame At the end of

This article is from WeChat official account. - Excel catalyst (ExcelCuiHuaJi)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2019-08-21

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

copyright notice
author[Excel catalyst],Please bring the original link to reprint, thank you.

Random recommended