Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Making Your Code Beautiful | Dealing With Conditions
Advanced Techniques in pandas

book
Making Your Code Beautiful

Let's make our code more convenient and more readable. By the way, it is essential to make your code understandable for your coworkers.

To simplify the code, we can write the condition first and then put it into the .loc[] function; look at the example from the previous chapter:

python
# The first way
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)]

# The second way
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)
condition_1 = data['est_diameter_min'] > 3.5
condition_2 = data['hazardous'] == True
data_extracted = data.loc[condition_1 & condition_2]

The first and second methods lead to the same output, but the second is much better for understanding because you can work with two conditions separately, and the statement within the .loc[] function takes up less space.

Taak

Swipe to start coding

Your task here is to consolidate knowledge from this chapter. You need to extract data on small asteroids with a high magnitude, or hazardous ones. To do so, follow the algorithm:

  1. Write the first condition: values from the column'est_diameter_min' are less than 0.01. Assign it to the variable condition_1.
  2. Write the second condition: values from the column 'absolute_magnitude' are greater than 20. Assign it to the variable condition_2.
  3. Write the third condition: values from the column 'hazardous' are equal to False. Assign it to the variable condition_3.
  4. Write the general condition that satisfies the requirement: (condition_1 and condition_2) or condition_3.

Oplossing

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)

# Write the first condition
condition_1 = data['est_diameter_min'] < 0.01
# Write the second condition
condition_2 = data['absolute_magnitude'] > 20
# Write the third condition
condition_3 = data['hazardous'] == False
# Write the general condition
data_extracted = data.loc[(condition_1 & condition_2) | condition_3]

print(data_extracted.head())

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
single

single

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)

# Write the first condition
condition_1 = data['___'] < ___
# Write the second condition
condition_2 = ___ 20
# Write the third condition
condition_3 = ___
# Write the general condition
data_extracted = data.loc[(condition_1 ___ condition_2) ___ condition_3]

print(data_extracted.head())

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt