pythonpandasfrequency-distributionfrequency-table

Creating a frequency distribution table in Python


I'm new to Python and Pandas, and i'm struggling to create a frequency distribution table form my df.

My dataframe is something like this:

Balances Weight
10 7
11 15
12 30
13 20
10 15
13 20

edit: The balance numbers are its respective ID

I need the frequency of each balance used (in this example, balance 10 would be 2 and so on) the min, max and mean of the measurements results.

I was to use df.groupby(['balances']) but how can i use the results form using df.groupby to creat a new table? Is that the way?


Solution

  • You don't need to use groupby, instead use Series.value_counts:

    In [1619]: df.Balances.value_counts()
    Out[1619]: 
    10    2
    13    2
    11    1
    12    1
    Name: Balances, dtype: int64
    

    To create another df, do this:

    In [1628]: df1 = df.Balances.value_counts().reset_index(name='Frequency').rename(columns={'index':'Balances'})
    
    In [1629]: df1
    Out[1629]: 
       Balances  Frequency
    0        10          2
    1        13          2
    2        11          1
    3        12          1