-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #628 from Klimatbyran/staging
Deploy to prod
- Loading branch information
Showing
9 changed files
with
355 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,42 @@ | ||
import numpy as np | ||
import pandas as pd | ||
|
||
|
||
PATH_BICYCLE_DATA = 'solutions/bicycles/cykelstatistik.xlsx' | ||
PATH_BICYCLE_DATA = 'solutions/bicycles/Cykelnät per komun 20231231.xlsx' | ||
PATH_POPULATION_DATA = 'solutions/bicycles/be0101_tabhel2023.xlsx' | ||
|
||
|
||
def bicycle_calculations(df): | ||
df_raw_bicycles = pd.read_excel(PATH_BICYCLE_DATA) | ||
df_raw_bicycles['metrePerCapita'] = df_raw_bicycles['Meter per capita'] | ||
df_bicycles = df_raw_bicycles.filter( | ||
['Kommun', 'metrePerCapita'], axis=1) | ||
df = df.merge(df_bicycles, on='Kommun', how='left') | ||
return df | ||
def calculate_bike_lane_per_capita(): | ||
""" | ||
Perform calculations on bicycle data and population data on municipality level. | ||
This function reads bicycle data and population data from Excel files, performs | ||
data cleaning and merging, and calculates the bike lane per capita per municipality. | ||
Returns: | ||
pandas.DataFrame: A DataFrame containing the merged data and the calculated bike lane per capita. | ||
""" | ||
|
||
df_raw_bicycles = pd.read_excel(PATH_BICYCLE_DATA, skiprows=3) | ||
df_bicycles = df_raw_bicycles[['Kommun', 'Totalsumma']] | ||
df_bicycles.loc[df_bicycles['Kommun'] == 'Malung', 'Kommun'] = 'Malung-Sälen' | ||
df_bicycles.loc[df_bicycles['Kommun'] == 'Upplands-Väsby', 'Kommun'] = 'Upplands Väsby' | ||
|
||
df_raw_population = pd.read_excel(PATH_POPULATION_DATA, skiprows=5) | ||
# Drop unnecessary rows | ||
df_population_drop = df_raw_population.drop([0, 1, 2, 3]) | ||
# Filter out county rows (that have 2 codes in the 'Kommun' column instead of 4) | ||
df_population_municipality = df_population_drop[df_population_drop['Kommun'].str.len() == 4] | ||
# Filter out unnecessary columns | ||
df_population_filter = df_population_municipality[['Kommunnamn', 'Folkmängd']] | ||
# Rename 'Kommunnamn' to 'Kommun' to match the bicycle dataframe | ||
df_population_renamed = df_population_filter.rename(columns={'Kommunnamn': 'Kommun'}) | ||
# Strip 'Kommun' column of whitespaces | ||
df_population_renamed['Kommun'] = df_population_renamed['Kommun'].str.strip() | ||
|
||
# Merge bicycle and population dataframes | ||
df_merged = df_bicycles.merge(df_population_renamed, on='Kommun', how='left') | ||
|
||
# Calculate bike lane per capita | ||
df_merged['bikeMetrePerCapita'] = df_merged['Totalsumma'] / df_merged['Folkmängd'] | ||
|
||
return df_merged[['Kommun', 'bikeMetrePerCapita']] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
import pandas as pd | ||
|
||
from solutions.bicycles.bicycle_data_calculations import calculate_bike_lane_per_capita | ||
|
||
|
||
class TestBicycleCalculations(unittest.TestCase): | ||
|
||
def test_calculate_bike_lane_per_capita(self): | ||
df_expected = pd.DataFrame( | ||
{ | ||
"Kommun": ["Ale", "Alingsås", "Alvesta"], | ||
"bikeMetrePerCapita": [91548/32446, 122012/42382, 66699/20040], | ||
} | ||
) | ||
|
||
df_result = calculate_bike_lane_per_capita() | ||
|
||
pd.testing.assert_frame_equal(df_result.iloc[:3], df_expected, check_dtype=False) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters