2

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?

Croyd
  • 39
  • 5
  • 3
    `df.groupby('Balances').describe()` (or `df.groupby('Balances')['Weight'].describe()`) will give you a lot of information for every column within the group – ALollz Apr 07 '21 at 15:14
  • With this i can create a new df based on the values given by .describe, right? – Croyd Apr 07 '21 at 18:31
  • 1
    Yes, if you assign it to something: `df1 = df.groupby('Balances').describe()`, then you have a DataFrame (with a column Multiindex) that you can select from or whatever – ALollz Apr 07 '21 at 19:04

2 Answers2

2

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
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

df.groupby(['balances']).count() should solve what you're looking for

cegarza
  • 135
  • 7