In [1]:
animals = ['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
print(animals)
for creature in animals:
print(creature)
In [2]:
for creature in animals:
pass
print('The loop variables is now: ' + creature)
In [3]:
import os
#os.mkdir('data/yearly_files')
In [4]:
os.listdir('data')
Out[4]:
In [5]:
import pandas as pd
surveys_df = pd.read_csv('data/surveys.csv')
surveys2002 = surveys_df[surveys_df.year == 2002]
surveys2002.to_csv('data/yearly_files/surveys2002.csv')
In [6]:
surveys_df['year'].unique()
Out[6]:
In [7]:
for year in surveys_df['year'].unique():
surveys_year = surveys_df[surveys_df.year == year]
filename = 'data/yearly_files/surveys' + str(year) + '.csv'
surveys_year.to_csv(filename)
In [8]:
def this_is_the_function_name(input_argument1, input_argument2):
print('The function arguments are:', input_argument1, input_argument2, '(this is done inside the function!)')
return input_argument1*input_argument2
In [9]:
product_of_inputs = this_is_the_function_name(2, 5)
print('Their product is:', product_of_inputs, '(this is done outside the function!)')
In [10]:
def one_year_csv_writer(this_year, all_data):
"""
Writes a csv file for data from a given year.
this_year --- year for which data is extracted
all_data --- DataFrame with multi-year data
"""
surveys_year = all_data[all_data.year == this_year]
filename = 'data/yearly_files/function_surveys' + str(this_year) + '.csv'
surveys_year.to_csv(filename)
In [11]:
one_year_csv_writer?
In [12]:
one_year_csv_writer(2002, surveys_df)
In [13]:
def yearly_data_csv_writer(start_year, end_year, all_data):
"""
Writes separate csv files for each year of data.
start_year --- the first year of data we want
end_year --- the last year of data we want
all_data --- DataFrame with multi-year data
"""
for year in range(start_year, end_year+1):
one_year_csv_writer(year, all_data)
In [14]:
surveys_df = pd.read_csv('data/surveys.csv')
yearly_data_csv_writer(1977, 2002, surveys_df)
In [15]:
def yearly_data_arg_test(all_data, start_year = 1977, end_year = 2002):
"""
Modified from yearly_data_csv_writer to test default argument values!
start_year --- the first year of data we want --- default: 1977
end_year --- the last year of data we want --- default: 2002
all_data --- DataFrame with multi-year data
"""
return start_year, end_year
start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
print('Both optional arguments:\t', start, end)
start,end = yearly_data_arg_test (surveys_df)
print('Default values:\t\t\t', start, end)
In [16]:
def yearly_data_arg_test(all_data, start_year = None, end_year = None):
"""
Modified from yearly_data_csv_writer to test default argument values!
start_year --- the first year of data we want --- default: None - check all_data
end_year --- the last year of data we want --- default: None - check all_data
all_data --- DataFrame with multi-year data
"""
if not start_year:
start_year = min(all_data.year)
if not end_year:
end_year = max(all_data.year)
return start_year, end_year
start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
print('Both optional arguments:\t', start, end)
start,end = yearly_data_arg_test (surveys_df)
print('Default values:\t\t\t', start, end)
In [17]:
a = 5
if a < 0:
print('a is a negative number')
elif a > 0:
print('a is a positive number')
else:
print('a must be zero!')
In [18]:
def yearly_data_arg_test(all_data, start_year = None, end_year = None):
"""
Modified from yearly_data_csv_writer to test default argument values!
start_year --- the first year of data we want --- default: None - check all_data
end_year --- the last year of data we want --- default: None - check all_data
all_data --- DataFrame with multi-year data
"""
if not start_year:
start_year = min(all_data.year)
if not end_year:
end_year = max(all_data.year)
return start_year, end_year
start,end = yearly_data_arg_test (surveys_df)
print('Default values:\t\t\t', start, end)
start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
print('No keywords:\t\t\t', start, end)
start,end = yearly_data_arg_test (surveys_df, start_year = 1988, end_year = 1993)
print('Both keywords, in order:\t', start, end)
start,end = yearly_data_arg_test (surveys_df, end_year = 1993, start_year = 1988)
print('Both keywords, flipped:\t\t', start, end)
start,end = yearly_data_arg_test (surveys_df, start_year = 1988)
print('One keyword, default end:\t', start, end)
start,end = yearly_data_arg_test (surveys_df, end_year = 1993)
print('One keyword, default start:\t', start, end)
In [19]:
for creature in animals:
In [20]:
ans = ''
for creature in animals:
if ans != '':
ans += ', '
ans += creature
print(ans)