Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Metodo Between | Estrazione dei Dati
Tecniche Avanzate in Pandas

bookMetodo Between

Esaminiamo un po' il nostro dataset. Abbiamo colonne numeriche, ad esempio 'Engine_volume'. Immagina di voler ottenere informazioni su tutte le auto con un 'Engine_volume' inferiore a 3, ma superiore a 2. Utilizzando l'istruzione .loc[], possiamo farlo facilmente.

Tuttavia, è utile sapere che Python fornisce una funzione speciale che consente di estrarre dati compresi tra due valori senza utilizzare due condizioni. Questo metodo si chiama .between(left_bound, right_bound). Puoi applicarlo alle colonne numeriche specificando i limiti sinistro e destro dei numeri. Osserva l'esempio e scopri come possiamo combinare i metodi .between() e .loc[].

Il codice seguente estrae i dati dove 'Engine_volume' >= 2 and 'Engine_volume' <= 3, ma cosa dovremmo fare per rendere uno o entrambi i limiti esclusivi? Scopriamolo utilizzando lo stesso esempio. Puoi aggiungere un argomento aggiuntivo al metodo .between().

  • .between(2, 3, inclusive = 'right') - estrae i dati dove 'Engine_volume' > 2 and 'Engine_volume' <= 3;
  • .between(2, 3, inclusive = 'left') - estrae i dati dove 'Engine_volume' >= 2 and 'Engine_volume' < 3;
  • .between(2, 3, inclusive = 'both') - estrae i dati dove 'Engine_volume' >= 2 and 'Engine_volume' <= 3. Il risultato sarà lo stesso che senza utilizzare inclusive = 'both';
  • .between(2, 3, inclusive = 'neither') - estrae i dati dove 'Engine_volume' > 2 and 'Engine_volume' < 3.
question-icon

Il compito qui è estrarre i dati corrispondenti ai commenti.

# Extract data where values from the column 'Year' are greater than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 3.03

bookMetodo Between

Scorri per mostrare il menu

Esaminiamo un po' il nostro dataset. Abbiamo colonne numeriche, ad esempio 'Engine_volume'. Immagina di voler ottenere informazioni su tutte le auto con un 'Engine_volume' inferiore a 3, ma superiore a 2. Utilizzando l'istruzione .loc[], possiamo farlo facilmente.

Tuttavia, è utile sapere che Python fornisce una funzione speciale che consente di estrarre dati compresi tra due valori senza utilizzare due condizioni. Questo metodo si chiama .between(left_bound, right_bound). Puoi applicarlo alle colonne numeriche specificando i limiti sinistro e destro dei numeri. Osserva l'esempio e scopri come possiamo combinare i metodi .between() e .loc[].

Il codice seguente estrae i dati dove 'Engine_volume' >= 2 and 'Engine_volume' <= 3, ma cosa dovremmo fare per rendere uno o entrambi i limiti esclusivi? Scopriamolo utilizzando lo stesso esempio. Puoi aggiungere un argomento aggiuntivo al metodo .between().

  • .between(2, 3, inclusive = 'right') - estrae i dati dove 'Engine_volume' > 2 and 'Engine_volume' <= 3;
  • .between(2, 3, inclusive = 'left') - estrae i dati dove 'Engine_volume' >= 2 and 'Engine_volume' < 3;
  • .between(2, 3, inclusive = 'both') - estrae i dati dove 'Engine_volume' >= 2 and 'Engine_volume' <= 3. Il risultato sarà lo stesso che senza utilizzare inclusive = 'both';
  • .between(2, 3, inclusive = 'neither') - estrae i dati dove 'Engine_volume' > 2 and 'Engine_volume' < 3.
question-icon

Il compito qui è estrarre i dati corrispondenti ai commenti.

# Extract data where values from the column 'Year' are greater than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3
some-alt