Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dealing With Several Conditions | Dealing With Conditions
Advanced Techniques in pandas
course content

Course Content

Advanced Techniques in pandas

Advanced Techniques in pandas

1. Getting Familiar With Indexing and Selecting Data
2. Dealing With Conditions
3. Extracting Data
4. Aggregating Data
5. Preprocessing Data

bookDealing With Several Conditions

Sometimes we need several conditions to be applied. For instance, we want to extract data on hazardous asteroids with a small minimum diameter. But how do we write two conditions simultaneously? Look at the table:

The example was included to help you deal with this topic. This code extracts data on large and hazardous asteroids, where the minimum estimated diameter is larger than 3.5 kilometers and 'hazardous' is True.

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] > 3.5) & (data['hazardous'] == True)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy these two conditions:

  • est_diameter_min > 3.5;
  • hazardous == True.

Look at the following example with the or statement. This code will extract data on extremely small or large asteroids with a minimum estimated diameter less than 0.0005 kilometers and a maximum estimated diameter larger than 20 kilometers:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] < 0.0005) | (data['est_diameter_max'] > 20)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy one of these two conditions:

  • est_diameter_min < 0.0005;
  • est_diameter_max > 20.
Task
test

Swipe to show code editor

Your task here is to extract data on very bright and not hazardous asteroids. The code should satisfy two conditions:

  • 'absolute_magnitude' is larger than or equal to 25;
  • 'hazardous' is False.

After this, output the random 5 rows of the data_extracted.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
toggle bottom row

bookDealing With Several Conditions

Sometimes we need several conditions to be applied. For instance, we want to extract data on hazardous asteroids with a small minimum diameter. But how do we write two conditions simultaneously? Look at the table:

The example was included to help you deal with this topic. This code extracts data on large and hazardous asteroids, where the minimum estimated diameter is larger than 3.5 kilometers and 'hazardous' is True.

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] > 3.5) & (data['hazardous'] == True)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy these two conditions:

  • est_diameter_min > 3.5;
  • hazardous == True.

Look at the following example with the or statement. This code will extract data on extremely small or large asteroids with a minimum estimated diameter less than 0.0005 kilometers and a maximum estimated diameter larger than 20 kilometers:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] < 0.0005) | (data['est_diameter_max'] > 20)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy one of these two conditions:

  • est_diameter_min < 0.0005;
  • est_diameter_max > 20.
Task
test

Swipe to show code editor

Your task here is to extract data on very bright and not hazardous asteroids. The code should satisfy two conditions:

  • 'absolute_magnitude' is larger than or equal to 25;
  • 'hazardous' is False.

After this, output the random 5 rows of the data_extracted.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
toggle bottom row

bookDealing With Several Conditions

Sometimes we need several conditions to be applied. For instance, we want to extract data on hazardous asteroids with a small minimum diameter. But how do we write two conditions simultaneously? Look at the table:

The example was included to help you deal with this topic. This code extracts data on large and hazardous asteroids, where the minimum estimated diameter is larger than 3.5 kilometers and 'hazardous' is True.

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] > 3.5) & (data['hazardous'] == True)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy these two conditions:

  • est_diameter_min > 3.5;
  • hazardous == True.

Look at the following example with the or statement. This code will extract data on extremely small or large asteroids with a minimum estimated diameter less than 0.0005 kilometers and a maximum estimated diameter larger than 20 kilometers:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] < 0.0005) | (data['est_diameter_max'] > 20)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy one of these two conditions:

  • est_diameter_min < 0.0005;
  • est_diameter_max > 20.
Task
test

Swipe to show code editor

Your task here is to extract data on very bright and not hazardous asteroids. The code should satisfy two conditions:

  • 'absolute_magnitude' is larger than or equal to 25;
  • 'hazardous' is False.

After this, output the random 5 rows of the data_extracted.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Sometimes we need several conditions to be applied. For instance, we want to extract data on hazardous asteroids with a small minimum diameter. But how do we write two conditions simultaneously? Look at the table:

The example was included to help you deal with this topic. This code extracts data on large and hazardous asteroids, where the minimum estimated diameter is larger than 3.5 kilometers and 'hazardous' is True.

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] > 3.5) & (data['hazardous'] == True)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy these two conditions:

  • est_diameter_min > 3.5;
  • hazardous == True.

Look at the following example with the or statement. This code will extract data on extremely small or large asteroids with a minimum estimated diameter less than 0.0005 kilometers and a maximum estimated diameter larger than 20 kilometers:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/planet', index_col = 0) data_extracted = data.loc[(data['est_diameter_min'] < 0.0005) | (data['est_diameter_max'] > 20)] print(data_extracted)
copy

In the output, you can see all the rows that satisfy one of these two conditions:

  • est_diameter_min < 0.0005;
  • est_diameter_max > 20.
Task
test

Swipe to show code editor

Your task here is to extract data on very bright and not hazardous asteroids. The code should satisfy two conditions:

  • 'absolute_magnitude' is larger than or equal to 25;
  • 'hazardous' is False.

After this, output the random 5 rows of the data_extracted.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt