0Pricing
Data Science Academy · Lezione

Raggruppare i numeri in categorie

cut e qcut per gli intervalli.

Raggruppare i numeri in categorie è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Why Bin a Number

Sometimes a raw number like age tells you less than a label like young or senior. Binning turns a continuous value into tidy groups. 🪣

Bins Reveal Patterns

Models and charts often read categories more clearly than exact figures. Grouping prices into low, mid, and high can surface trends you would otherwise miss.

Meet pandas cut

The cut function slices a column into bins you define by edges. You give it the boundaries, and it labels each value for you.

import pandas as pd
ages = pd.Series([7, 22, 45, 70])
bins = pd.cut(ages, [0, 18, 60, 100])

Name Your Bins

Pass labels so each bin has a friendly name instead of an interval. Now your output reads like a real category, not a math range.

pd.cut(ages, [0, 18, 60, 100],
       labels=['child', 'adult', 'senior'])

Edges Are Inclusive Right

By default cut includes the right edge of each bin and excludes the left. So a value of 18 lands in the first bin, not the second.

Flip With right=False

Want the left edge included instead? Set right=False and the boundary behavior flips, so 18 moves into the next bin up.

pd.cut(ages, [0, 18, 60, 100], right=False)

Equal-Width Bins

Give cut a single number instead of edges and it creates that many equal-width bins automatically across the value range.

pd.cut(ages, 4)

Meet pandas qcut

The qcut function splits by quantiles, so each bin holds roughly the same number of rows. Great when your data is lopsided.

pd.qcut(ages, 4)

cut vs qcut

Use cut for fixed, meaningful edges like price tiers. Use qcut when you want balanced groups such as quartiles of income.

Bins Become a Feature

The result is a categorical column you can drop straight into your DataFrame and feed to grouping, plotting, or a model.

df['age_group'] = pd.cut(df['age'],
                         [0, 18, 60, 100])

Watch the Out-of-Range

Values outside your bin edges become NaN. Always set edges that cover your real minimum and maximum, or you will silently lose rows.

Quick Check

You want four groups that each hold about the same number of rows. Which tool fits?

Recap: Binning

You learned to group numbers into labels: cut for chosen edges and equal widths, qcut for equal counts. Mind the edges and you turn raw values into useful features. 🎉

Domande Frequenti

La lezione «Raggruppare i numeri in categorie» è gratuita?

Sì — il testo completo di «Raggruppare i numeri in categorie» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.

Cosa imparerò in «Raggruppare i numeri in categorie»?

cut e qcut per gli intervalli. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Data Science Academy?

Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Raggruppare i numeri in categorie»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Data Science Academy?

Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Raggruppare i numeri in categorie
  2. Codificare le colonne categoriche
  3. Scalare e normalizzare i numeri
  4. Creare feature da date e testo
← Torna a Data Science Academy