1- ---
2- layout: page
3- title: "Extra Challenges"
4- permalink: /extra_challenges/
5- ---
1+ ---
2+ title : " Extra Challenges"
3+ ---
64
75# Extra Challenges
86
9- A collection of challenges that have been either removed from or not (yet) added to the main lesson.
7+ A collection of challenges that have been either removed from or not (yet) added to the main lesson.
108
119> ## Looping Over DataFrame
1210>
@@ -15,23 +13,23 @@ A collection of challenges that have been either removed from or not (yet) added
1513> The file ` surveys.csv ` in the ` data ` folder contains 25 years of data from surveys,
1614> starting from 1977. We can extract data corresponding to each year in this DataFrame
1715> to individual CSV files, by using a ` for ` loop:
18- >
16+ >
1917> ~~~
2018> import pandas as pd
21- >
19+ >
2220> # Load the data into a DataFrame
2321> surveys_df = pd.read_csv('data/surveys.csv')
24- >
22+ >
2523> # Loop through a sequence of years and export selected data
2624> start_year = 1977
2725> end_year = 2002
2826> for year in range(start_year, end_year+1):
2927>
3028> # Select data for the year
31- > surveys_year = surveys_df[surveys_df.year == year]
29+ > surveys_year = surveys_df[surveys_df.year == year]
3230>
3331> # Write the new DataFrame to a CSV file
34- > filename = 'data/surveys' + str(year) + '.csv'
32+ > filename = 'data/surveys' + str(year) + '.csv'
3533> surveys_year.to_csv(filename)
3634> ~~~
3735> {: .language-python}
@@ -41,6 +39,6 @@ A collection of challenges that have been either removed from or not (yet) added
4139>
4240> > ## Solution
4341> > We get the expected files for all years between 1977 and 2002,
44- > > plus an empty `data/surveys1976.csv` file with only the headers.
45- > {: .solution}
42+ > > plus an empty `data/surveys1976.csv` file with only the headers.
43+ > {: .solution}
4644{: .challenge}
0 commit comments