Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

python - Multi Level Filtering and Frequency count of a series in a dataframe

I currently have following:

   Department   Semester      Grade   
      D1         Fall-2019        A
      D1         Fall-2019        B
      D1         Spring-2020      A
      D1         Spring-2020      A
      D1         Spring-2019      C
      D1         Spring-2019      C
      D1         Fall-2019        A
      D1         Fall-2019        A

Department is constant, I want the each grade count in new column per semester level.

Essentially the output I am looking for is:

   Department   Semester      Grade A   Grade B Grade C
      D1         Fall-2019        3        1     0
      D1         Spring-2019      0        0     2 
      D1         Spring-2020      2        0     0

Any help would be greatly appreciated!

Edit: As an extension to this question: If I need to derive the below table from the original table how do I go about?

   Department   Semester      GradeType1   GradeType2 GradeType3
      D1         Fall-2019       GradeA      GradeB     NaN
      D1         Spring-2019     NaN         NaN       GradeC 
      D1         Spring-2020     GradeA      NaN       Nan

Thanks in advance!!!

question from:https://stackoverflow.com/questions/65887370/multi-level-filtering-and-frequency-count-of-a-series-in-a-dataframe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is crosstab:

(pd.crosstab([df['Department'], df['Semester']], df['Grade'])
   .add_prefix('Grade ')
   .reset_index()
)

which is a short-hand for groupby().value_counts() then unstack:

(df.groupby(['Department','Semester'])['Grade']
   .value_counts()
   .unstack(fill_value=0)
   .reset_index()
)

Output:

Grade Department     Semester  Grade A  Grade B  Grade C
0             D1    Fall-2019        3        1        0
1             D1  Spring-2019        0        0        2
2             D1  Spring-2020        2        0        0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...