animals = ['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
print(animals)
for i in animals:
print(i)
for i in animals:
for i in animals:
print(i,end=",")
import os
os.mkdir('data/yearly_files') # equivalent to mkdir in shell
os.listdir('data') # equivalent to ls in shell
import pandas as pd
surveys_df = pd.read_csv('data/surveys.csv')
surveys_df['year'].unique()
for year in surveys_df['year'].unique():
filename='data/yearly_files/surveys' + str(year) + '.csv'
print(filename)
for year in surveys_df['year'].unique():
# Select data for the year
surveys_year = surveys_df[surveys_df.year == year]
# Write the new DataFrame to a CSV file
filename = 'data/yearly_files/surveys' + str(year) + '.csv'
surveys_year.to_csv(filename)
for year in surveys_df['year'].unique():
# Select data for the year
surveys_df_noNA = surveys_df[~pd.isnull(surveys_df).any(axis=1)]
surveys_year = surveys_df_noNA[surveys_df_noNA.year == year]
# Write the new DataFrame to a CSV file
filename = 'data/yearly_files/surveys' + str(year) + '.csv'
surveys_year.to_csv(filename)
for year in surveys_df['year'].unique()[::5]:
# Select data for the year
surveys_df_noNA = surveys_df[~pd.isnull(surveys_df).any(axis=1)]
surveys_year = surveys_df_noNA[surveys_df_noNA.year == year]
# Write the new DataFrame to a CSV file
filename = 'data/yearly_files/surveys' + str(year) + '.csv'
print(filename)
surveys_df.head()
import os
os.mkdir('data/species_files') # equivalent to mkdir in shell
os.listdir('data') # equivalent to ls in shell
for i in surveys_df['species_id'].unique():
# Select data for the year
surveys_df_noNA = surveys_df[~pd.isnull(surveys_df).any(axis=1)]
surveys_species = surveys_df_noNA[surveys_df_noNA.species_id == i]
# Write the new DataFrame to a CSV file
filename = 'data/species_files/surveys' + str(i) + '.csv'
surveys_species.to_csv(filename)
print(filename)
def this_is_the_function_name(input_argument1, input_argument2):
# The body of the function is indented
# This function prints the two arguments to screen
print('The function arguments are:', input_argument1, input_argument2, '(this is done inside the function!)')
# And returns their product
return input_argument1 * input_argument2
this_is_the_function_name(1,2)
this_is_the_function_name(56,25)
this_is_the_function_name(1)
def this_is_the_function_name(input_argument1, input_argument2):
# The body of the function is indented
# This function prints the two arguments to screen
print('The function arguments are:', input_argument1, input_argument2, '(this is done inside the function!)')
x = input_argument1 + input_argument2
# And returns their product
return input_argument1 * input_argument2
this_is_the_function_name(5,10)
x
x = 5
this_is_the_function_name(5,10)
x
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
"""
# Select data for the year
surveys_year = all_data[all_data.year == this_year]
# Write the new DataFrame to a csv file
filename = 'data/yearly_files/function_surveys' + str(this_year) + '.csv'
surveys_year.to_csv(filename)
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
"""
# "end_year" is the last year of data we want to pull, so we loop to end_year+1
for year in range(start_year, end_year+1):
one_year_csv_writer(year, all_data)
def one_year_csv_writer(this_year, all_data, pathdir, rootid):
"""
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
pathdir -- Path to the export directory
rootid -- Basename for the files to be exported
"""
# Select data for the year
surveys_year = all_data[all_data.year == this_year]
# Write the new DataFrame to a csv file
filename = 'pathdir/rootid' + str(this_year) + '.csv'
surveys_year.to_csv(filename)
def one_year_csv_writer(this_year, all_data, pathdir, rootid):
"""
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
pathdir -- Path to the export directory
rootid -- Basename for the files to be exported
"""
# Select data for the year
surveys_year = all_data[all_data.year == this_year]
# Write the new DataFrame to a csv file
filename = 'pathdir/rootid' + str(this_year) + '.csv'
surveys_year.to_csv(filename)
def one_year_csv_writer(this_year, all_data, pathdir, rootid):
"""
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
pathdir -- Path to the export directory
rootid -- Basename for the files to be exported
"""
# Select data for the year
surveys_year = all_data[all_data.year == this_year]
# Write the new DataFrame to a csv file
filename = pathdir + '/' + rootid + str(this_year) + '.csv'
surveys_year.to_csv(filename)
return(filename)
filename
def yearly_data_arg_test(all_data, start_year=None, end_year=None):
"""
Modified from yearly_data_csv_writer to test default argument values!
all_data -- DataFrame with multi-year data
start_year -- the first year of data we want, Check all_data! (default None)
end_year -- the last year of data we want; Check all_data! (default None)
"""
if start_year is None:
start_year = min(all_data.year)
if end_year is None:
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)
x = None
type(x)
def yearly_data_arg_test(all_data, start_year, end_year):
"""
Modified from yearly_data_csv_writer to test default argument values!
all_data -- DataFrame with multi-year data
start_year -- the first year of data we want, Check all_data! (default None)
end_year -- the last year of data we want; Check all_data! (default None)
"""
if start_year is None:
start_year = min(all_data.year)
if end_year is None:
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)
def yearly_data_arg_test(all_data, start_year=1977, end_year=None):
"""
Modified from yearly_data_csv_writer to test default argument values!
all_data -- DataFrame with multi-year data
start_year -- the first year of data we want, Check all_data! (default None)
end_year -- the last year of data we want; Check all_data! (default None)
"""
if start_year is None:
start_year = min(all_data.year)
if end_year is None:
end_year = max(all_data.year)
return start_year, end_year
start, end = yearly_data_arg_test(surveys_df, end_year=1993)
print('Both optional arguments:\t', start, end)
def one_year_csv_writer(this_year, all_data, pathdir='data/yearly_files', rootid='surveys'):
"""
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
pathdir -- Path to the export directory
rootid -- Basename for the files to be exported
"""
# Select data for the year
surveys_year = all_data[all_data.year == this_year]
# Write the new DataFrame to a csv file
filename = pathdir + '/' + rootid + str(this_year) + '.csv'
return(filename)
one_year_csv_writer(1998, all_data=surveys_df)
def one_year_csv_writer(this_year, all_data, pathdir='data/yearly_files', rootid='surveys'):
"""
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
pathdir -- Path to the export directory
rootid -- Basename for the files to be exported
"""
if this_year in surveys_df['year'].unique():
surveys_year = all_data[all_data.year == this_year]
filename = pathdir + '/' + rootid + str(this_year) + '.csv'
return(filename)
else:
print('There is no data available for', str(this_year))
one_year_csv_writer(2100, all_data=surveys_df)
import os
def one_year_csv_writer(this_year, all_data, pathdir='yearly_files', rootid='surveys'):
"""
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
pathdir -- Path to the export directory
rootid -- Basename for the files to be exported
"""
if pathdir in os.listdir('.'):
print('Processed directory exists')
else:
os.mkdir(pathdir)
print('Processed directory created')
if this_year in surveys_df['year'].unique():
surveys_year = all_data[all_data.year == this_year]
filename = 'data/'+ pathdir + '/' + rootid + str(this_year) + '.csv'
surveys_year.to_csv(filename)
print(filename)
else:
print('There is no data available for', str(this_year))
one_year_csv_writer(1977, all_data=surveys_df)
year_list = surveys_df['year'].unique()
for i in year_list:
# Select data for the year
surveys_df_noNA = surveys_df[~pd.isnull(surveys_df).any(axis=1)]
surveys_species = surveys_df_noNA[surveys_df_noNA.species_id == i]
# Write the new DataFrame to a CSV file
filename = 'data/species_files/surveys' + str(i) + '.csv'
print(filename)