Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Recall Loops | Explore Dataset
Introduction to Python for Data Analysis

book
Recall Loops

Imagine that you want to count how many 'Data Scientist' jobs are in the dataset. You are already familiar with the loops and if/else statements. Try it.

Compito

Swipe to start coding

Follow the algorithm:

  1. Create a for loop that iterates through the 'job_title' column in the df.
  2. Within the for loop, implement the if statement that checks if i is equal to 'Data Scientist'.
  3. Within the if statement, increase the count variable by 1.

Soluzione

import pandas as pd

df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/INTRO+to+Python/ds_salaries.csv', index_col = 0)

count = 0

# Create a for loop
for i in df['job_title']:
# Create the if statement
if i == 'Data Scientist':
# Increase count varible
count = count + 1
print(count)

In this chapter, you count values in a straightforward but irrational way. Imagine that you have thousands of rows in the dataset; such a loop may take even an hour to process, depending on the dataset's size and the computer's power. Thus, in the next section, you will learn how to do the same thing but in a way that pandas offer you.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 12
single

single

import pandas as pd


df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/INTRO+to+Python/ds_salaries.csv', index_col = 0)

count = 0

# Create a for loop
___ i ___['___']:
# Create the if statement
___ i ___ '___':
# Increase count varible
count = ___
print(count)

Chieda ad AI

expand

Chieda ad AI

ChatGPT

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

some-alt