135-4 data cleaning walkthrough

https://app.dataquest.io/m/136/data-cleaning-walkthrough/4/reading-in-the-data

hi,
i am struggling to understand this line of code here.

can someone explain to me what does the /{0}.format(f) in the following liner does
(“schools/{0}”.format(f))

import pandas as pd
data_files = [
    "ap_2010.csv",
    "class_size.csv",
    "demographics.csv",
    "graduation.csv",
    "hs_directory.csv",
    "sat_results.csv"
]
data = {}
for f in data_files:
    d = pd.read_csv("schools/{0}".format(f))
    key_name = f.replace(".csv", "")
    data[key_name] = d

not sure if i run through this before. have checked previous missions but don’t recall seeing this :grinning:

Hi @willx
If your question is about how the str.format() method work, in general, you can check this mission inserting-variables-into-strings.
In your code you have a list of data files data_files and you want to read every file in this list, you can use loop to do it, in the first iteration of your loop, f will be equal to "ap_2010.csv"
which means that the string "schools/{0}".format(f) " will be same as "schools/ap_2010.csv" by defintion of str.format() method, and this code d = pd.read_csv("schools/ap_2010.csv") will read your first data file.

Hi @bahmed21,
sorry for late reply.
can i understand why is the {0} needed? why in particular the number zero?

rgds

Hi @willx
You don’t have to be sorry
In the str.format() method you can use integers between the brace characters ( {} ) if you wont to specify order or repetition. In your code

you don’t need to specify any order or repetition you can just use empty brace like this
pd.read_csv("schools/{}".format(f))
you can always check this mission inserting-variables-into-strings for more explanation and example.

ah… i must forgotten this part when i did this mission back then of the position of the arguments.
was wondering about that part there
geez… thanks so much bahmed :grinning:

1 Like