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:
- Create a
for
loop that iterates through the'job_title'
column in thedf
. - Within the
for
loop, implement theif
statement that checks ifi
is equal to'Data Scientist'
. - Within the
if
statement, increase thecount
variable by 1.
Soluzione
99
1
2
3
4
5
6
7
8
9
10
11
12
13
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?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 12
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione