PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] How can I customize the output of this .groupby operation done on this DataFrame in Python?

 August 17, 2022     dataframe, output, pandas, pandas-groupby, python     No comments   

Issue

I am working with a DataFrame to create a frequency distribution by counting the three types of values in one column. In this example, I'm counting and displaying each person's "personal status". When I execute the code, all of the other columns are displayed with the count repeated in each column. I'd like the count of each value to be displayed once without a column heading. What do I need to do to accomplish this?

creditData.groupby(['Personal_Status']).count()

Here's an image of my output: Current Output

Edit: Here's what I'd like the output to look like: Desired Output


Solution

What's recommended in the documentation is to use Named aggregation

import pandas as pd
animals = pd.DataFrame(
     {
         "kind": ["cat", "dog", "cat", "dog"],
         "height": [9.1, 6.0, 9.5, 34.0],
         "weight": [7.9, 7.5, 9.9, 198.0],
     }
 )

animals.groupby('kind').agg(**{
    '':('height','count')
})

This will get you

kind    
cat 2
dog 2

For reference https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html (search for named aggregation)



Answered By - Bertrand
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing