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?

3
  • 3
    df.groupby('Balances').describe() (or df.groupby('Balances')['Weight'].describe()) will give you a lot of information for every column within the group Commented Apr 7, 2021 at 15:14
  • With this i can create a new df based on the values given by .describe, right? Commented Apr 7, 2021 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 Commented Apr 7, 2021 at 19:04

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

3 Comments

and to add the min, max and mean i would follow the same logic?
For that you'll have to use groupby.
actually that is not what i needed, it helped a bit tho. But thanks
1

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

Comments

-1

Class Interval f x (Midpoint) cf rf% <cf% fx 0 32-36 4 34 4 5.71 5.71 136 1 37-41 8 39 12 11.43 17.14 312 2 42-46 15 44 27 21.43 38.57 660 3 47-51 19 49 46 27.14 65.71 931 4 52-56 11 54 57 15.71 81.43 594 Class Interval f x (Midpoint) cf rf% <cf% fx 0 32-36 4 34 4 5.71 5.71 136 1 37-41 8 39 12 11.43 17.14 312 2 42-46 15 44 27 21.43 38.57 660 3 47-51 19 49 46 27.14 65.71 931 4 52-56 11 54 57 15.71 81.43 594successI've completed and displayed the frequency distribution table with all calculated columns. Let me know if you have questions or need further calculations, such as solving for the mean, median, mode, or creating graphs

1 Comment

please mark code with "Code sample" markup (Ctrl-K). The formatting is quite bad, it doesn't even look like python!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.