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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.