File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ layout: page
3+ title: "Extra Challenges"
4+ permalink: /extra_challenges/
5+ ---
6+
7+ # Extra Challenges
8+
9+ A collection of challenges that have been either removed from or not (yet) added to the main lesson.
10+
11+ > ## Looping Over DataFrame
12+ >
13+ > (Please refer to lesson ` 06-loops-and-functions.md ` )
14+ >
15+ > The file ` surveys.csv ` in the ` data ` folder contains 25 years of data from surveys,
16+ > starting from 1977. We can extract data corresponding to each year in this DataFrame
17+ > to individual CSV files, by using a ` for ` loop:
18+ >
19+ > ~~~
20+ > import pandas as pd
21+ >
22+ > # Load the data into a DataFrame
23+ > surveys_df = pd.read_csv('data/surveys.csv')
24+ >
25+ > # Loop through a sequence of years and export selected data
26+ > start_year = 1977
27+ > end_year = 2002
28+ > for year in range(start_year, end_year+1):
29+ >
30+ > # Select data for the year
31+ > surveys_year = surveys_df[surveys_df.year == year]
32+ >
33+ > # Write the new DataFrame to a CSV file
34+ > filename = 'data/surveys' + str(year) + '.csv'
35+ > surveys_year.to_csv(filename)
36+ > ~~~
37+ > {: .language-python}
38+ >
39+ > What happens if there is no data for a year in a sequence? For example,
40+ > imagine we used `1976` as the `start_year`
41+ >
42+ > > ## Solution
43+ > > 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}
46+ {: .challenge}
You can’t perform that action at this time.
0 commit comments