search
Search
Unlock 100+ guides
search toc
close
Outline
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# Getting frequency counts of values in intervals in Pandas Series

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

To get the frequency counts of values that fall under some intervals, first use Pandas' `cut(~)` method to partition the values into bins (intervals), and then use `value_counts(~)` to get the corresponding frequency counts.

For instance:

``` s = pd.Series([5,4,9,7,3])out = pd.cut(s, bins=[1,4,8,10])out.value_counts() (4, 8] 2(1, 4] 2(8, 10] 1dtype: int64 ```

To break this down, the return value of `cut(~)` is a Series where each value is assigned the corresponding interval:

``` s = pd.Series([5,4,9,7,3])out = pd.cut(s, bins=[1,4,8,10]) # returns a Series, where each value is an Intervalout 0 (4, 8]1 (1, 4]2 (8, 10]3 (4, 8]4 (1, 4]dtype: categoryCategories (3, interval[int64]): [(1, 4] < (4, 8] < (8, 10]] ```

Here, `(4, 8]` represents the interval strictly larger than `4` but less than or equal to `8`. The first value `5` (represented by index `0`) falls in this interval `(4, 8]`.

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
3
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!