-
-
Notifications
You must be signed in to change notification settings - Fork 1
Fold in the six high_dim_data datasets (PR B1) #62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 contains hidden or 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,81 @@ | ||
| --- | ||
| jupytext: | ||
| text_representation: | ||
| extension: .md | ||
| format_name: myst | ||
| format_version: 0.13 | ||
| jupytext_version: 1.14.1 | ||
| kernelspec: | ||
| display_name: Python 3 (ipykernel) | ||
| language: python | ||
| name: python3 | ||
| --- | ||
|
|
||
| Regarding converting between ``.ipynb`` and ``.md`` please refer to https://manual.quantecon.org/writing/converting.html | ||
|
|
||
| ```{code-cell} ipython3 | ||
| import pandas as pd | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| var_list = ['yearmerge', # 3-year window | ||
| 'ffanw', # net wealth (ffafin + ffanfin - tdebt) | ||
| 'tinc', # total household income, excluding capital gains | ||
| 'incws', # income from wages, salaries and self-employment | ||
| 'wgtI95W95', # survey weight | ||
| 'ffanwgroups', # wealth groups | ||
| 'tincgroups'] # income groups | ||
| ``` | ||
|
|
||
| Rename the variables needed. | ||
|
|
||
| ```{code-cell} ipython3 | ||
| var_names_new = 'year', 'n_wealth', 't_income', 'l_income', 'weights', 'nw_groups', 'ti_groups' | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| df = pd.read_stata('https://github.com/QuantEcon/high_dim_data/blob/main/SCF_plus/SCF_plus.dta?raw=true') | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| df = df[[*var_list]] | ||
| df1=df.astype({'yearmerge': int}).dropna() | ||
| df1.columns = var_names_new | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| df1 | ||
| ``` | ||
|
|
||
| Export the dataset with weights. | ||
|
|
||
| ```{code-cell} ipython3 | ||
| # df1.to_csv('SCF_plus_mini.csv', index=None) # use it when you want to export the weighted data | ||
| ``` | ||
|
|
||
| Generate and export the dataset without weights. | ||
|
|
||
| ```{code-cell} ipython3 | ||
| counts = list(round(df1['weights'])) | ||
| df1["weights"] = counts | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| df2 = df1.loc[df1.index.repeat(df1.weights)].reset_index(drop=True) | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| df2 = df2.drop(columns=['weights']) | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| df2 | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
| # df2.to_csv('SCF_plus_mini_no_weights.csv', index=None) # use it when you want to export the non weighted data | ||
| ``` | ||
|
|
||
| ```{code-cell} ipython3 | ||
|
|
||
| ``` | ||
This file contains hidden or 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,156 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# parse_forbeslists\n", | ||
| "\n", | ||
| "This notebook \n", | ||
| "- parses Forbes richest lists and Forbes global 2000 list and\n", | ||
| "- saves them as csv files." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import requests\n", | ||
| "import pandas as pd\n", | ||
| "from pathlib import Path\n", | ||
| "from pandas import DataFrame" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 2, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Forbes lists\n", | ||
| "lists = [ \n", | ||
| " { 'type': 'person', 'year': 2020, 'uri': 'billionaires' }, # World richest\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'forbes-400' }, # American richest 400\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'hong-kong-billionaires' }, # Hong Kong richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'australia-billionaires' }, # Australia richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'china-billionaires' }, # China richest 400\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'taiwan-billionaires' }, # Taiwan richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'india-billionaires' }, # India richest 100\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'japan-billionaires' }, # Japan richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'africa-billionaires' }, # Africa richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'korea-billionaires' }, # Korea richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'malaysia-billionaires' }, # Malaysia richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'philippines-billionaires' }, # Philippines richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'singapore-billionaires' }, # Singapore richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'indonesia-billionaires' }, # Indonesia richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'thailand-billionaires' }, # Thailand richest 50\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'self-made-women' }, # American richest self-made women\n", | ||
| " # { 'type': 'person', 'year': 2018, 'uri': 'richest-in-tech' }, # tech richest\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'hedge-fund-managers' }, # hedge fund highest-earning\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'powerful-people' }, # world powerful\n", | ||
| " # { 'type': 'person', 'year': 2020, 'uri': 'power-women' }, # world powerful women\n", | ||
| " # { 'type': 'person', 'year': 0, 'uri': 'rtb' }, # real-time world billionaires\n", | ||
| " # { 'type': 'person', 'year': 0, 'uri': 'rtrl' }, # real-time American richest 400\n", | ||
| "]\n", | ||
| "\n", | ||
| "url = 'http://www.forbes.com/ajax/list/data'\n", | ||
| "SOURCES_DIR = Path('./sources')\n", | ||
| "\n", | ||
| "for forbes_list in lists:\n", | ||
| " response = requests.get(url, params=forbes_list)\n", | ||
|
mmcky marked this conversation as resolved.
|
||
| "\n", | ||
| " if not SOURCES_DIR.exists():\n", | ||
| " SOURCES_DIR.mkdir(exist_ok=True, parents=True)\n", | ||
| "\n", | ||
| " DataFrame(response.json()).to_csv('forbes-{}.csv'.format(forbes_list['uri']))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Then Forbes Global 2000 for the largest 2000 firms globally." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 3, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "headers = {\n", | ||
| " \"accept\": \"application/json, text/plain, */*\",\n", | ||
| " \"referer\": \"https://www.forbes.com/global2000/\",\n", | ||
| " \"user-agent\": \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36\",\n", | ||
| "}\n", | ||
| "\n", | ||
| "cookies = {\n", | ||
| " \"notice_behavior\": \"expressed,eu\",\n", | ||
| " \"notice_gdpr_prefs\": \"0,1,2:1a8b5228dd7ff0717196863a5d28ce6c\",\n", | ||
| "}\n", | ||
| "\n", | ||
| "api_url = \"https://www.forbes.com/forbesapi/org/global2000/2020/position/true.json?limit=2000\"\n", | ||
| "response = requests.get(api_url, headers=headers, cookies=cookies).json()\n", | ||
| "\n", | ||
| "sample_table = [\n", | ||
| " [\n", | ||
| " item[\"organizationName\"],\n", | ||
| " item[\"country\"],\n", | ||
| " item[\"revenue\"],\n", | ||
| " item[\"profits\"],\n", | ||
| " item[\"assets\"],\n", | ||
| " item[\"marketValue\"]\n", | ||
| " ] for item in\n", | ||
| " sorted(response[\"organizationList\"][\"organizationsLists\"], key=lambda k: k[\"position\"])\n", | ||
| "]\n", | ||
| "\n", | ||
| "dfff = pd.DataFrame(sample_table, columns=[\"Company\", \"Country\", \"Sales\", \"Profits\", \"Assets\", \"Market Value\"])\n", | ||
| "dfff.to_csv('forbes-global2000.csv')" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.10.9" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.