-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[UP][m] Updating the scripts with v2 source of world bank #3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7096451
[UP][m] Updating the scripts with v2 source of world bank
gradedSystem a53c580
[remove-update][s] Moved README.md to the root folder
gradedSystem c4ea44e
[update][s] Update README
gradedSystem aab7dc4
[update][s] Updating the README.md
gradedSystem 99c8186
[update][s] Update README.md
gradedSystem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
This file was deleted.
Oops, something went wrong.
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,35 +1,69 @@ | ||
#!/usr/bin/python | ||
|
||
import csv, os, sys | ||
import numpy as np | ||
import os, csv | ||
import requests | ||
import zipfile | ||
import tempfile | ||
import pandas as pd | ||
|
||
# Building query to fetch data from API | ||
apiBase = "http://api.worldbank.org/indicator/" | ||
apiIndicator = "SI.POV.GINI" # This can be changed to any other indicator | ||
FILE_NAME = 'gini-index.csv' | ||
source = apiBase+apiIndicator+"?format=csv" | ||
print(source) | ||
|
||
def main(): | ||
giniIndex = pd.read_csv(source) | ||
giniIndex.to_csv('archive/gini-index.csv', sep=",", index=False) | ||
print("Saved archive CSV file.") | ||
print(giniIndex) | ||
|
||
# Processing the data | ||
df = pd.read_csv('archive/gini-index.csv') # Reading the source csv | ||
""" | ||
Python is printing "Country Name" with quotes in data frame and does not | ||
work for the remaining code | ||
""" | ||
df.columns.values[0] = 'Country Name' | ||
|
||
df = pd.melt(df, id_vars=['Country Name', 'Country Code'], var_name="Year", value_name="Value") # Unpivoting | ||
df = df.sort_values(by=['Country Name', 'Year'], ascending=[True, True]) # Sorting by country | ||
|
||
df.dropna().to_csv('data/gini-index.csv', sep=",", index=False) # Saving CSV | ||
print ("File has been saved and it is ready for data packaging.") | ||
tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix='.zip') | ||
tmpdir = tempfile.TemporaryDirectory() | ||
|
||
API_INDICATOR = "SI.POV.GINI" | ||
SOURCE_URL = f"https://api.worldbank.org/v2/en/indicator/{API_INDICATOR}?downloadformat=csv" | ||
ARCHIVE_FILE = 'archive/gini-index.csv' | ||
OUTPUT_FILE = 'data/gini-index.csv' | ||
|
||
def download_zip_file(): | ||
response = requests.get(SOURCE_URL) | ||
|
||
with open(tmpfile.name, 'wb') as d: | ||
d.write(response.content) | ||
|
||
with zipfile.ZipFile(tmpfile.name, 'r') as zip_ref: | ||
zip_ref.extractall(tmpdir.name) | ||
|
||
os.unlink(tmpfile.name) | ||
|
||
for path in os.scandir(tmpdir.name): | ||
if path.is_file() and 'metadata' not in path.name.lower(): | ||
filename = os.path.join(tmpdir.name, path.name) | ||
archive_path = os.path.join('archive', 'gini-index.csv') | ||
|
||
# Ensure the archive folder exists | ||
os.makedirs('archive', exist_ok=True) | ||
|
||
os.rename(filename, archive_path) | ||
print(f"File saved to: {archive_path}") | ||
|
||
def process_population_data(filename, output_file): | ||
# Read the raw CSV file | ||
with open(filename) as fo: | ||
lines = [row for row in csv.reader(fo)] | ||
|
||
# Extract headings and data rows | ||
headings = lines[4] | ||
lines = lines[5:] | ||
|
||
# Define output structure | ||
outheadings = ['Country Name', 'Country Code', 'Year', 'Value'] | ||
outlines = [] | ||
|
||
# Process each row and reshape the data | ||
for row in lines: | ||
for idx, year in enumerate(headings[4:]): | ||
if row[idx + 4]: # Check if the value exists | ||
value = row[idx + 4] | ||
outlines.append(row[:2] + [int(year), value]) | ||
|
||
df = pd.DataFrame(outlines, columns=outheadings) | ||
|
||
df = df.sort_values(by=['Country Name', 'Year']) | ||
df.to_csv(output_file, index=False) | ||
print(f"Processed data saved to {output_file}") | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
main() | ||
download_zip_file() | ||
process_population_data(ARCHIVE_FILE, OUTPUT_FILE) | ||
|
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,3 +1,2 @@ | ||
process.py | ||
pandas | ||
numpy | ||
pandas==2.2.3 | ||
requests==2.32.3 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after this add how to run script for example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done @Mikanebu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update to
make
pls