简体   繁体   中英

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

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

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)

This would probably do the trick:

(
    creditData[["Age", "Personal_Status"]]
    .groupby(["Personal_Status"]).count()
    .rename({"Age": ""}, axis="columns"
)

You can make a new column with no name (empty string here), then select that one and remake the result to a frame to look like what you want.

creditData[''] = 1
creditData.groupby(['Personal_Status']).count()[''].to_frame()

I think creditData["Personal_Status"].value_counts() would be the best solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM