search
Search
Unlock 100+ guides
search toc
close
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

# Binning values in a 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 bin the values of a Series, use the Pandas' `cut(~)` method.

# Equal-width bins

To segment the values in the Series into `3` equal-width bins:

``` s = pd.Series([5,4,9,7])pd.cut(s, bins=3) # returns a Series 0 (3.995, 5.667]1 (3.995, 5.667]2 (7.333, 9.0]3 (5.667, 7.333]dtype: categoryCategories (3, interval[float64]): [(3.995, 5.667] < (5.667, 7.333] < (7.333, 9.0]] ```

Here, `(3.995,5.667]` represents the interval `3.995 < a < 5.667`.

# Custom bins

You could also specify your own bins:

``` s = pd.Series([5,4,9,7])pd.cut(s, bins=[2,6,10]) 0 (2, 6]1 (2, 6]2 (6, 10]3 (6, 10]dtype: categoryCategories (2, interval[int64]): [(2, 6] < (6, 10]] ```

# Labelling bins

We can label numeric intervals using the `labels` parameter:

``` s = pd.Series([5,4,9,7])pd.cut(s, bins=[2,6,10], labels=["A","B"]) 0 A1 A2 B3 Bdtype: categoryCategories (2, object): ['A' < 'B'] ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!