{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"toc_visible":true,"authorship_tag":"ABX9TyNIifLNYBGnMZPHD8irkQMi"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# U.S. Population Migration Data:\n","### Strengths and Limitations\n","\n","####
Census Bureau Processing
\n","\n"," The first step in creating the migration data file is to assign a geographic code (geocode) to the IMF data. The Census assigns these geocodes based on “ZIP plus 4” codes and State of residence reported on the tax return. The “plus 4” codes actually consist of a pair of two-character codes—a sector code and a segment code. According to U.S. Post Office guidelines, each sector code identifies a single county. Using the combination of ZIP sector codes and State of residence codes for each individual return, Census assigns each record a State/county geocode. To prepare the migration data, which examine year-to-year changes, Census must geocode 2 consecutive filing years of IMF data. County equivalent codes are assigned to the District of Columbia, the Virgin Islands, Puerto Rico, APO/FPO (military), and “other foreign” areas.\n","\t\n","####
Identifying Migrants
\n","\n"," Once the geographic codes are assigned, Census determines who in the file has, or has not, migrated. To do this; \n"," - first, coded returns for the current filing year are matched to coded returns filed during the prior year. \n"," \n"," - The mailing addresses on the two returns are then compared to one another focusing on: (1) the street address and (2) State plus ZIP code. \n"," \n"," - If the two are identical, the return is labeled a “non-migrant.”\n"," \n"," -If any of the above information changed between the 2 years, the return is considered a mover. However, the return is only classified a “migrant” if the taxpayer’s geographic code also changed from one year to the next.\n"," \n"," - For cases in which the geographic code did change from one year to the next, a taxpayer is considered an “in-migrant” for the address on the return filed in the current filing year, and an “out-migrant” for the address on the return filed for the prior year. \n","\n","####
Limitations and Margin of Error in dataset:
\n","\n"," Although the filer’s return address determines the migration status of the record, there are instances for which the taxpayer may not have changed residences but the return address suggests a move. This may happen if: \n"," 1. the filing address is that of a financial institution or tax preparer, and not that of the actual taxpayer; \n"," 2. the taxpayer is a college student living away from home who filed with a home address one year and the college address another; \n"," 3. the taxpayer reports his or her place of business as the return address; \n"," 4. the taxpayer maintains dual residences, primarily residing in one county but filing the tax return from the other; or \n"," 5. the taxpayer uses a post office box for mailing purposes.\n","\n","As mentioned, those who are not required to file United States Federal income tax returns are not included in this file, and so the data under-represent the poor and the elderly. Also excluded is the small percentage of tax returns filed after late September of the filing year. Most taxpayers whose returns are filed after this date have been granted an extension to file by the IRS. These taxpayers are likely to have complex returns that report relatively high income, and so the migration data set may under-represent the very wealthy, as well.\n","\n"," The matching process also causes some returns to be excluded from the counts. When the current-year tax return is compared to the prior-year tax return, only the Social Security Number of the primary taxpayer is considered. If a secondary filer exists (as in the case of a married couple filing jointly), that Social Security Number is not recorded or compared in creating the migration dataset. If, for example, a husband and wife file a joint return in the prior year, but divorce and file separately in the current year, only the husband’s current-year return will have a match with the prior-year return. The now ex-wife’s current-year return becomes a non-match and will not be included in the data counts. Other changes in filing status—from from joint to married filing separately—will also affect the data.\n","\n"],"metadata":{"id":"Sk_VvjGhjGq3"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"eJS5MLmPQvpB"},"outputs":[],"source":["from google.colab import drive\n","drive.mount('/content/gdrive')\n","import glob as glob\n","import pandas as pd\n","!pip install xlrd --upgrade\n","!pip install patool"]},{"cell_type":"code","source":["!pip install patool\n","import patoolib"],"metadata":{"id":"rCmXu6LSBzgO"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# ETL Inflow Files"],"metadata":{"id":"l36uoytFeYGb"}},{"cell_type":"markdown","source":["Unzip rar file "],"metadata":{"id":"qwDsL_7f_Kzp"}},{"cell_type":"markdown","source":["\n","\n","1. Read invdividual files for all states\n","2. Process and Clean \n","3. Merge All\n","\n","\n","\n"],"metadata":{"id":"Kafg6VvC_OoR"}},{"cell_type":"code","source":["def read_xls(path_xls):\n"," \"\"\"read xls file, remove header rows, rename columns\"\"\"\n"," df = pd.read_excel(path_xls, engine='xlrd',skiprows=8, header=None , names=['destination_state', 'destination_county', 'origin_state','origin_county',\n"," 'origin_usps','county_names', 'numb_returns', 'numb_exemptions','agi'])\n"," return df\n","\n","\n","def set_fips(df):\n"," \"\"\"Calculate county fips using state and county area code \"\"\"\n"," cleaned_df = pd.DataFrame()\n"," cleaned_df['origin_countyfips'] = df.origin_state*1000 + df.origin_county\n"," cleaned_df['destination_countyfips'] = df.destination_state*1000 + df.destination_county\n"," cleaned_df['county'] = df.county_names\n"," cleaned_df['returns'] = df.numb_returns\n"," cleaned_df['exemptions'] = df.numb_exemptions\n"," return cleaned_df\n","\n","\n","def inflow_state(cleaned_df, format=1):\n"," \"\"\"Merge migrant and non migrant datasets to calculate %Inflow.\"\"\"\n","\n"," if format==1:\n"," migrant = cleaned_df[cleaned_df.origin_countyfips == 1].reset_index(drop=True)\n"," migrant.rename(columns={'returns':'inmigrant_returns', 'exemptions':'inmigrant_exemptions'}, inplace=True)\n"," migrant.drop(columns=['origin_countyfips'], inplace=True)\n","\n"," nonmigrant = cleaned_df[cleaned_df.origin_countyfips==63050].reset_index(drop=True)\n"," nonmigrant.rename(columns={'returns':'nonmigrant_returns', 'exemptions':'nonmigrant_exemptions'}, inplace=True)\n"," nonmigrant.drop(columns=['origin_countyfips', 'county'], inplace=True)\n"," \n"," elif format==2:\n"," migrant = cleaned_df[cleaned_df.origin_countyfips==96000].reset_index(drop=True).drop(columns='origin_countyfips')\n"," migrant.rename(columns={'returns':'inmigrant_returns', 'exemptions':'inmigrant_exemptions'}, inplace=True)\n"," migrant = migrant[~(migrant.county=='Total Mig - US & For')].reset_index(drop=True)\n","\n"," nonmigrant = cleaned_df[cleaned_df.destination_countyfips==cleaned_df.origin_countyfips].reset_index(drop=True).drop(columns=['origin_countyfips','county'])\n"," nonmigrant.rename(columns={'returns':'nonmigrant_returns', 'exemptions':'nonmigrant_exemptions'}, inplace=True)\n","\n"," inflow = migrant.merge(nonmigrant, on='destination_countyfips', how='left')\n"," inflow = inflow[~(inflow.inmigrant_returns=='d') | ~(inflow.nonmigrant_returns=='d')].reset_index(drop=True)\n"," inflow.inmigrant_returns = inflow.inmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," inflow.nonmigrant_returns = inflow.nonmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," inflow['%inflow'] = 100*(inflow['inmigrant_returns']/inflow['nonmigrant_returns'])\n"," return inflow\n","\n","\n","def process_year(xls_files, format=1):\n","\n"," \"\"\"Process and merge all states for a given Year\"\"\"\n"," inflow_allstates = pd.DataFrame()\n"," for xls_file in xls_files:\n"," df = read_xls(xls_file)\n"," cleaned_df = set_fips(df)\n"," inflow_df = inflow_state(cleaned_df, format)\n"," inflow_allstates = inflow_allstates.append(inflow_df)\n"," return inflow_allstates"],"metadata":{"id":"Kb28PBBjfeT2","executionInfo":{"status":"ok","timestamp":1668724910475,"user_tz":-300,"elapsed":808,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":151,"outputs":[]},{"cell_type":"markdown","source":["## Format 1 parsing from Year 1993-2005"],"metadata":{"id":"7P_ahsqLo0bh"}},{"cell_type":"code","source":["path_raw = \"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw\"\n","zip_files_path = glob.glob(path_raw + \"/*.zip\")\n","years = list(range(1993,2005))\n","unzip_path = \"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped/\"\n","\n","for zippath in zip_files_path:\n"," patoolib.extract_archive(zippath,outdir=unzip_path)"],"metadata":{"id":"oxaEJ2QD-57E"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["unzipped_folders = glob.glob(\"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped/*/*Inflow\")"],"metadata":{"id":"yi2tzNjgGNlN"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["unzipped_folders9295 = unzipped_folders[0:3]\n","years = [1993,1994,1995]\n","i=0\n","inflow_9295 = pd.DataFrame()\n","for unzipped_folder in unzipped_folders9295:\n"," xls_files_i = glob.glob(unzipped_folder + \"/*.xls\")\n"," inflow_i = process_year(xls_files_i)\n"," inflow_i['Year'] = years[i]\n"," inflow_9295 = inflow_9295.append(inflow_i)\n"," i = i+1\n","inflow_9295 = inflow_9295.reset_index(drop=True)\n","inflow_9295 = inflow_9295[~(inflow_9295['nonmigrant_exemptions'].isna())].reset_index(drop=True)\n","inflow_9295.inmigrant_exemptions = inflow_9295.inmigrant_exemptions.astype('float64')\n","inflow_9295.nonmigrant_exemptions = inflow_9295.nonmigrant_exemptions.astype('float64')"],"metadata":{"id":"zvTzBTLSPGIA"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["inflow_9295.append(inflow_on95).reset_index(drop=True).to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_inflow1993_2004.csv', index=False)"],"metadata":{"id":"TfBwZBEWqsny"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["inflow_9295 = pd.read_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_inflow1993_2004.csv')"],"metadata":{"id":"fxgzzHXwGRyQ","executionInfo":{"status":"ok","timestamp":1668697154223,"user_tz":-300,"elapsed":1121,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":31,"outputs":[]},{"cell_type":"code","source":["#@title Annual Migration Inflow returns filed, 1993-1995\n","df = inflow_9295.copy()\n","df['County Code'] = df.destination_countyfips.astype('str').str.zfill(5)\n","fig = px.choropleth(df, locations=\"County Code\",\n"," geojson=counties,\n"," color=\"%inflow\",\n"," hover_name=\"county\",\n"," scope=\"usa\",\n"," animation_frame='Year',\n"," range_color=(0, 30),\n"," color_continuous_scale=\"ylorrd\")\n"," \n","fig.update_layout(showlegend=False, title='Annual Migration Inflow returns filed, 1993-1995')\n","fig.update_traces(marker_line_width=0.25)\n","fig.show()"],"metadata":{"id":"M91d5_rbGHYM","executionInfo":{"status":"ok","timestamp":1668697407457,"user_tz":-300,"elapsed":64988,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":34,"outputs":[]},{"cell_type":"markdown","source":["## Format 2 parsing from Year 1996-2004"],"metadata":{"id":"KhPPpttLo7VJ"}},{"cell_type":"code","source":["unzipped_folder95onw = unzipped_folders[3:]\n","years = [1998,1997,1996,2000,1999,2003,2002,2001,2004]\n","i=0\n","inflow_on95 = pd.DataFrame()\n","for unzipped_folder in unzipped_folder95onw:\n"," xls_files_i = glob.glob(unzipped_folder + \"/*.xls\")\n"," inflow_i = process_year(xls_files_i, format=2)\n"," inflow_i['Year'] = years[i]\n"," inflow_on95 = inflow_on95.append(inflow_i)\n"," i = i+1\n","inflow_on95 = inflow_on95.reset_index(drop=True)\n","inflow_on95 = inflow_on95[~(inflow_on95['nonmigrant_exemptions'].isna())].reset_index(drop=True)\n","inflow_on95.inmigrant_exemptions = inflow_on95.inmigrant_exemptions.astype('float64')\n","inflow_on95.nonmigrant_exemptions = inflow_on95.nonmigrant_exemptions.astype('float64')"],"metadata":{"id":"_MMG52vIjQII"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["inflow_9304 = inflow_9295.append(inflow_on95).reset_index(drop=True)"],"metadata":{"id":"X6MzGNIC8SR0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["inflow_on95.info()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"-CPF2WLa2-Xy","executionInfo":{"status":"ok","timestamp":1668475425802,"user_tz":-300,"elapsed":819,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"c7dfb9c4-e29d-4807-b566-554499419cbb"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["\n","RangeIndex: 28170 entries, 0 to 28169\n","Data columns (total 8 columns):\n"," # Column Non-Null Count Dtype \n","--- ------ -------------- ----- \n"," 0 destination_countyfips 28170 non-null int64 \n"," 1 county 28170 non-null object \n"," 2 inmigrant_returns 28170 non-null float64\n"," 3 inmigrant_exemptions 28170 non-null object \n"," 4 nonmigrant_returns 28168 non-null float64\n"," 5 nonmigrant_exemptions 28168 non-null object \n"," 6 %inflow 28168 non-null float64\n"," 7 Year 28170 non-null int64 \n","dtypes: float64(3), int64(2), object(3)\n","memory usage: 1.7+ MB\n"]}]},{"cell_type":"markdown","source":["## Format 3 Dat Files 2005-2011"],"metadata":{"id":"Qbqt0mZt7Y8y"}},{"cell_type":"code","source":["path_raw = \"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw\"\n","zip_files_path = glob.glob(path_raw + \"/county*.zip\")\n","years = list(range(2005,2012))\n","unzip_path = \"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/\"\n","\n","for zippath in zip_files_path:\n"," patoolib.extract_archive(zippath,outdir=unzip_path)\n","unzipped_folders = glob.glob(\"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyin*.dat\")\n","unzipped_folders"],"metadata":{"id":"LYM77g4-8r00"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["years = [2005,2006,2007,2010,2009,2011]\n","i=0\n","inflow_2004on = pd.DataFrame()\n","for unzip_dat in unzipped_folders:\n"," dat_data = pd.read_fwf(unzip_dat, header=None,encoding = \"ISO-8859-1\", names=['destination_state', 'destination_county', 'origin_state','origin_county',\n"," 'origin_usps','county_names', 'numb_returns', 'numb_exemptions','agi', 'unknown'])\n"," dat_data.origin_countyfips = dat_data.origin_state*1000 + dat_data.origin_county\n"," dat_data.destination_countyfips = dat_data.destination_state*1000 + dat_data.destination_county\n"," cleaned_df = set_fips(dat_data)\n"," process_df = inflow_state(cleaned_df, format=2)\n"," process_df['Year'] = years[i]\n"," i=i+1\n"," inflow_2004on = inflow_2004on.append(process_df)\n","inflow_2004on = inflow_2004on.reset_index(drop=True)\n","inflow_2004on.nunique()"],"metadata":{"id":"gCibNscp-05g"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["inflow93_2011 = inflow_9304.append(inflow_2004on).reset_index(drop=True)"],"metadata":{"id":"u5H__GGPA6OC"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["inflow93_2011.describe()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":364},"id":"FGbuSNauA_vu","executionInfo":{"status":"ok","timestamp":1668477969500,"user_tz":-300,"elapsed":656,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"67de9482-9847-4f81-9e76-decd6df6cfe5"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" destination_countyfips inmigrant_returns inmigrant_exemptions \\\n","count 56426.000000 56426.000000 56426.000000 \n","mean 30403.395509 2215.937103 4206.826764 \n","std 15162.050856 5761.534846 10322.898247 \n","min 1001.000000 1.000000 -1.000000 \n","25% 18179.000000 244.000000 518.000000 \n","50% 29175.000000 563.000000 1166.000000 \n","75% 45083.000000 1543.000000 3082.000000 \n","max 57007.000000 110030.000000 193646.000000 \n","\n"," nonmigrant_returns nonmigrant_exemptions %inflow Year \n","count 5.642600e+04 5.642600e+04 56426.000000 56426.000000 \n","mean 3.032793e+04 6.679927e+04 7.903940 2001.674175 \n","std 9.692266e+04 2.119741e+05 4.383086 5.439342 \n","min 1.000000e+00 -1.000000e+00 1.780415 1993.000000 \n","25% 3.403000e+03 7.934000e+03 5.722452 1997.000000 \n","50% 7.765000e+03 1.790950e+04 7.096774 2002.000000 \n","75% 2.036775e+04 4.606325e+04 9.045557 2006.000000 \n","max 3.290970e+06 7.244412e+06 100.000000 2011.000000 "],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
destination_countyfipsinmigrant_returnsinmigrant_exemptionsnonmigrant_returnsnonmigrant_exemptions%inflowYear
count56426.00000056426.00000056426.0000005.642600e+045.642600e+0456426.00000056426.000000
mean30403.3955092215.9371034206.8267643.032793e+046.679927e+047.9039402001.674175
std15162.0508565761.53484610322.8982479.692266e+042.119741e+054.3830865.439342
min1001.0000001.000000-1.0000001.000000e+00-1.000000e+001.7804151993.000000
25%18179.000000244.000000518.0000003.403000e+037.934000e+035.7224521997.000000
50%29175.000000563.0000001166.0000007.765000e+031.790950e+047.0967742002.000000
75%45083.0000001543.0000003082.0000002.036775e+044.606325e+049.0455572006.000000
max57007.000000110030.000000193646.0000003.290970e+067.244412e+06100.0000002011.000000
\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":307}]},{"cell_type":"code","source":["inflow93_2011.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/inflow1993_2011.csv', index=False)"],"metadata":{"id":"mhlGw4R9CV8v"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## Format 4 - Latest 2012-2020"],"metadata":{"id":"_vcYruAUFNWI"}},{"cell_type":"code","source":["path_raw = \"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw\"\n","zip_files_path = glob.glob(path_raw + \"/*migrationdata.zip\")"],"metadata":{"id":"AmsRzXYpFRtM"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["unzip_path = \"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/\"\n","\n","for zippath in zip_files_path:\n"," patoolib.extract_archive(zippath,outdir=unzip_path)"],"metadata":{"id":"i5ee12oZFdU2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["unzipped_folders = glob.glob(\"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow*.csv\")\n","unzipped_folders"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"KEYTdEqyFgOA","executionInfo":{"status":"ok","timestamp":1668479336024,"user_tz":-300,"elapsed":673,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"e0b7b7c7-af62-49cf-a45f-9c632f9a10d4"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1112.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1314.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1213.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1617.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1516.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1415.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1819.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1718.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyinflow1920.csv']"]},"metadata":{},"execution_count":316}]},{"cell_type":"code","source":["years = [2012,2014,2013,2017,2016,2015,2019,2018,2020]\n","i=0\n","inflow_1221 = pd.DataFrame()\n","for csv_path in unzipped_folders:\n","\n"," df = pd.read_csv(csv_path, encoding = \"ISO-8859-1\")\n"," cleaned_df = pd.DataFrame()\n"," cleaned_df['origin_countyfips'] = df.y1_statefips*1000 + df.y1_countyfips\n"," cleaned_df['destination_countyfips'] = df.y2_statefips*1000 + df.y2_countyfips\n"," cleaned_df['county'] = df.y1_countyname\n"," cleaned_df['returns'] = df.n1\n"," cleaned_df['individuals'] = df.n2\n"," migrant = cleaned_df[cleaned_df.origin_countyfips==96000].reset_index(drop=True).drop(columns='origin_countyfips')\n"," migrant.rename(columns={'returns':'inmigrant_returns', 'individuals':'inmigrants'}, inplace=True)\n"," migrant = migrant[~(migrant.county == 'Total Migration-US and Foreign')].reset_index(drop=True)\n"," nonmigrant = cleaned_df[cleaned_df.destination_countyfips==cleaned_df.origin_countyfips].reset_index(drop=True).drop(columns=['origin_countyfips','county'])\n"," nonmigrant.rename(columns={'returns':'nonmigrant_returns', 'individuals':'nonmigrants'}, inplace=True)\n"," inflow = migrant.merge(nonmigrant, on='destination_countyfips', how='left')\n"," inflow = inflow[~(inflow.inmigrant_returns=='d') | ~(inflow.nonmigrant_returns=='d')].reset_index(drop=True)\n"," inflow.inmigrant_returns = inflow.inmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," inflow.nonmigrant_returns = inflow.nonmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," inflow['%inflow'] = 100*(inflow['inmigrant_returns']/inflow['nonmigrant_returns'])\n"," inflow['Year'] = years[i]\n"," inflow_1221 = inflow_1221.append(inflow)\n"," i=i+1\n","\n","inflow_1221.reset_index(drop=True)\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":641},"id":"-is1998BGhcC","executionInfo":{"status":"ok","timestamp":1668480338455,"user_tz":-300,"elapsed":2160,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"b2400374-cd71-451b-e241-314adef46529"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" destination_countyfips \\\n","0 1001 \n","1 1003 \n","2 1005 \n","3 1007 \n","4 1009 \n","... ... \n","28172 56037 \n","28173 56039 \n","28174 56041 \n","28175 56043 \n","28176 56045 \n","\n"," county inmigrant_returns \\\n","0 Autauga County Total Migration-US and Foreign 2006.0 \n","1 Baldwin County Total Migration-US and Foreign 5633.0 \n","2 Barbour County Total Migration-US and Foreign 665.0 \n","3 Bibb County Total Migration-US and Foreign 479.0 \n","4 Blount County Total Migration-US and Foreign 1257.0 \n","... ... ... \n","28172 Sweetwater County Total Migration-US and Foreign 833.0 \n","28173 Teton County Total Migration-US and Foreign 1275.0 \n","28174 Uinta County Total Migration-US and Foreign 475.0 \n","28175 Washakie County Total Migration-US and Foreign 205.0 \n","28176 Weston County Total Migration-US and Foreign 160.0 \n","\n"," inmigrants nonmigrant_returns nonmigrants %inflow Year \n","0 4618 17696.0 41757.0 11.335895 2012 \n","1 11518 65215.0 144258.0 8.637583 2012 \n","2 1440 8001.0 17972.0 8.311461 2012 \n","3 1062 6198.0 14840.0 7.728299 2012 \n","4 2767 17390.0 42898.0 7.228292 2012 \n","... ... ... ... ... ... \n","28172 1673 15424.0 35193.0 5.400674 2020 \n","28173 1981 10840.0 20640.0 11.761993 2020 \n","28174 1083 7102.0 17922.0 6.688257 2020 \n","28175 396 2885.0 6592.0 7.105719 2020 \n","28176 326 2452.0 5267.0 6.525285 2020 \n","\n","[28177 rows x 8 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
destination_countyfipscountyinmigrant_returnsinmigrantsnonmigrant_returnsnonmigrants%inflowYear
01001Autauga County Total Migration-US and Foreign2006.0461817696.041757.011.3358952012
11003Baldwin County Total Migration-US and Foreign5633.01151865215.0144258.08.6375832012
21005Barbour County Total Migration-US and Foreign665.014408001.017972.08.3114612012
31007Bibb County Total Migration-US and Foreign479.010626198.014840.07.7282992012
41009Blount County Total Migration-US and Foreign1257.0276717390.042898.07.2282922012
...........................
2817256037Sweetwater County Total Migration-US and Foreign833.0167315424.035193.05.4006742020
2817356039Teton County Total Migration-US and Foreign1275.0198110840.020640.011.7619932020
2817456041Uinta County Total Migration-US and Foreign475.010837102.017922.06.6882572020
2817556043Washakie County Total Migration-US and Foreign205.03962885.06592.07.1057192020
2817656045Weston County Total Migration-US and Foreign160.03262452.05267.06.5252852020
\n","

28177 rows × 8 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":332}]},{"cell_type":"code","source":["inflow_all = inflow93_2011.drop(columns=['inmigrant_exemptions', 'nonmigrant_exemptions']).append(inflow_1221.drop(columns=['inmigrants', 'nonmigrants']))\n","inflow_all\n","inflow_all.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/inflow_all.csv', index=False)\n","inflow_1221.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/inflow_1221.csv', index=False)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":423},"id":"APX9-XszHTWZ","executionInfo":{"status":"ok","timestamp":1668480544346,"user_tz":-300,"elapsed":610,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"6f005dcf-32fe-4b4b-d495-407e5965474e"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" destination_countyfips \\\n","0 57001 \n","1 57003 \n","2 57005 \n","3 57007 \n","4 56001 \n","... ... \n","3097 56037 \n","3098 56039 \n","3099 56041 \n","3100 56043 \n","3101 56045 \n","\n"," county inmigrant_returns \\\n","0 Foreign / Overseas 35338.0 \n","1 Foreign / Puerto Rico 6691.0 \n","2 APO / FPO Zip Code 94040.0 \n","3 Foreign / Virgin Islands 3211.0 \n","4 Albany 1561.0 \n","... ... ... \n","3097 Sweetwater County Total Migration-US and Foreign 833.0 \n","3098 Teton County Total Migration-US and Foreign 1275.0 \n","3099 Uinta County Total Migration-US and Foreign 475.0 \n","3100 Washakie County Total Migration-US and Foreign 205.0 \n","3101 Weston County Total Migration-US and Foreign 160.0 \n","\n"," nonmigrant_returns %inflow Year \n","0 151429.0 23.336349 1993 \n","1 67648.0 9.890906 1993 \n","2 169479.0 55.487701 1993 \n","3 5102.0 62.936103 1993 \n","4 9384.0 16.634697 1993 \n","... ... ... ... \n","3097 15424.0 5.400674 2020 \n","3098 10840.0 11.761993 2020 \n","3099 7102.0 6.688257 2020 \n","3100 2885.0 7.105719 2020 \n","3101 2452.0 6.525285 2020 \n","\n","[84603 rows x 6 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
destination_countyfipscountyinmigrant_returnsnonmigrant_returns%inflowYear
057001Foreign / Overseas35338.0151429.023.3363491993
157003Foreign / Puerto Rico6691.067648.09.8909061993
257005APO / FPO Zip Code94040.0169479.055.4877011993
357007Foreign / Virgin Islands3211.05102.062.9361031993
456001Albany1561.09384.016.6346971993
.....................
309756037Sweetwater County Total Migration-US and Foreign833.015424.05.4006742020
309856039Teton County Total Migration-US and Foreign1275.010840.011.7619932020
309956041Uinta County Total Migration-US and Foreign475.07102.06.6882572020
310056043Washakie County Total Migration-US and Foreign205.02885.07.1057192020
310156045Weston County Total Migration-US and Foreign160.02452.06.5252852020
\n","

84603 rows × 6 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":337}]},{"cell_type":"code","source":["inflow_all = pd.read_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/inflow_all.csv')\n","inflow_all"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":554},"id":"BSEP6BtCxRS0","executionInfo":{"status":"ok","timestamp":1668725157622,"user_tz":-300,"elapsed":596,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"af3136ac-c4c1-45a5-e03c-0a65d61aa4c6"},"execution_count":155,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" destination_countyfips \\\n","0 57001 \n","1 57003 \n","2 57005 \n","3 57007 \n","4 56001 \n","... ... \n","84598 56037 \n","84599 56039 \n","84600 56041 \n","84601 56043 \n","84602 56045 \n","\n"," county inmigrant_returns \\\n","0 Foreign / Overseas 35338.0 \n","1 Foreign / Puerto Rico 6691.0 \n","2 APO / FPO Zip Code 94040.0 \n","3 Foreign / Virgin Islands 3211.0 \n","4 Albany 1561.0 \n","... ... ... \n","84598 Sweetwater County Total Migration-US and Foreign 833.0 \n","84599 Teton County Total Migration-US and Foreign 1275.0 \n","84600 Uinta County Total Migration-US and Foreign 475.0 \n","84601 Washakie County Total Migration-US and Foreign 205.0 \n","84602 Weston County Total Migration-US and Foreign 160.0 \n","\n"," nonmigrant_returns %inflow Year \n","0 151429.0 23.336349 1993 \n","1 67648.0 9.890906 1993 \n","2 169479.0 55.487701 1993 \n","3 5102.0 62.936103 1993 \n","4 9384.0 16.634697 1993 \n","... ... ... ... \n","84598 15424.0 5.400674 2020 \n","84599 10840.0 11.761993 2020 \n","84600 7102.0 6.688257 2020 \n","84601 2885.0 7.105719 2020 \n","84602 2452.0 6.525285 2020 \n","\n","[84603 rows x 6 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
destination_countyfipscountyinmigrant_returnsnonmigrant_returns%inflowYear
057001Foreign / Overseas35338.0151429.023.3363491993
157003Foreign / Puerto Rico6691.067648.09.8909061993
257005APO / FPO Zip Code94040.0169479.055.4877011993
357007Foreign / Virgin Islands3211.05102.062.9361031993
456001Albany1561.09384.016.6346971993
.....................
8459856037Sweetwater County Total Migration-US and Foreign833.015424.05.4006742020
8459956039Teton County Total Migration-US and Foreign1275.010840.011.7619932020
8460056041Uinta County Total Migration-US and Foreign475.07102.06.6882572020
8460156043Washakie County Total Migration-US and Foreign205.02885.07.1057192020
8460256045Weston County Total Migration-US and Foreign160.02452.06.5252852020
\n","

84603 rows × 6 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":155}]},{"cell_type":"code","source":["#@title Append data from 2008 dat file and remove outlier\n","\n","inflow_all = pd.read_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/inflow_all.csv')\n","\n","inflow_2008 = pd.DataFrame()\n","unzip_dat = '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/ci0708us.dat'\n","dat_data = pd.read_fwf(unzip_dat, header=None,encoding = \"ISO-8859-1\", names=['destination_state', 'destination_county', 'origin_state','origin_county',\n"," 'origin_usps','county_names', 'numb_returns', 'numb_exemptions','agi', 'unknown'])\n","dat_data.origin_countyfips = dat_data.origin_state*1000 + dat_data.origin_county\n","dat_data.destination_countyfips = dat_data.destination_state*1000 + dat_data.destination_county\n","cleaned_df = set_fips(dat_data)\n","process_df = inflow_state(cleaned_df, format=2)\n","process_df['Year'] = 2008\n","i=i+1\n","inflow_2008 = inflow_2008.append(process_df)\n","inflow_2008 = inflow_2008.reset_index(drop=True)\n","inflow_2008.nunique()\n"],"metadata":{"id":"fjlsEaUUOg_s","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1668724960909,"user_tz":-300,"elapsed":2327,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"19079513-603e-487e-f0fc-72d26653efc0"},"execution_count":152,"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:9: UserWarning:\n","\n","Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access\n","\n","/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:10: UserWarning:\n","\n","Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access\n","\n"]},{"output_type":"execute_result","data":{"text/plain":["destination_countyfips 3142\n","county 1875\n","inmigrant_returns 1805\n","inmigrant_exemptions 2230\n","nonmigrant_returns 2976\n","nonmigrant_exemptions 3056\n","%inflow 3128\n","Year 1\n","dtype: int64"]},"metadata":{},"execution_count":152}]},{"cell_type":"code","source":["inflow_all = inflow_all.append(inflow_2008.drop(columns=['inmigrant_exemptions','nonmigrant_exemptions'])).reset_index(drop=True)\n","inflow_all = inflow_all.sort_values(by='Year').reset_index(drop=True)"],"metadata":{"id":"uvVvLSFVwlCu","executionInfo":{"status":"ok","timestamp":1668725215062,"user_tz":-300,"elapsed":350,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":156,"outputs":[]},{"cell_type":"code","source":["inflow_all[(inflow_all.inmigrant_returns==1)]"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":554},"id":"BAyoRMZCzl_2","executionInfo":{"status":"ok","timestamp":1668725791292,"user_tz":-300,"elapsed":371,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"10d0ebd1-9f0a-4504-86d4-089b766feff3"},"execution_count":165,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" destination_countyfips \\\n","37759 38087 \n","38110 30103 \n","38117 31005 \n","38120 31009 \n","38142 30069 \n","... ... \n","80737 8017 \n","80755 8053 \n","81061 28055 \n","81184 20071 \n","81220 20199 \n","\n"," county inmigrant_returns \\\n","37759 Slope County Tot Mig-US & For 1.0 \n","38110 Treasure County Tot Mig-US & Fo 1.0 \n","38117 Arthur County Tot Mig-US & For 1.0 \n","38120 Blaine County Tot Mig-US & For 1.0 \n","38142 Petroleum Count Tot Mig-US & Fo 1.0 \n","... ... ... \n","80737 Cheyenne County Total Migration-US and Foreign 1.0 \n","80755 Hinsdale County Total Migration-US and Foreign 1.0 \n","81061 Issaquena County Total Migration-US and Foreign 1.0 \n","81184 Greeley County Total Migration-US and Foreign 1.0 \n","81220 Wallace County Total Migration-US and Foreign 1.0 \n","\n"," nonmigrant_returns %inflow Year \n","37759 1.0 0.0 2005 \n","38110 1.0 0.0 2005 \n","38117 1.0 0.0 2005 \n","38120 1.0 0.0 2005 \n","38142 1.0 0.0 2005 \n","... ... ... ... \n","80737 635.0 0.0 2018 \n","80755 291.0 0.0 2018 \n","81061 324.0 0.0 2018 \n","81184 482.0 0.0 2018 \n","81220 565.0 0.0 2018 \n","\n","[284 rows x 6 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
destination_countyfipscountyinmigrant_returnsnonmigrant_returns%inflowYear
3775938087Slope County Tot Mig-US & For1.01.00.02005
3811030103Treasure County Tot Mig-US & Fo1.01.00.02005
3811731005Arthur County Tot Mig-US & For1.01.00.02005
3812031009Blaine County Tot Mig-US & For1.01.00.02005
3814230069Petroleum Count Tot Mig-US & Fo1.01.00.02005
.....................
807378017Cheyenne County Total Migration-US and Foreign1.0635.00.02018
807558053Hinsdale County Total Migration-US and Foreign1.0291.00.02018
8106128055Issaquena County Total Migration-US and Foreign1.0324.00.02018
8118420071Greeley County Total Migration-US and Foreign1.0482.00.02018
8122020199Wallace County Total Migration-US and Foreign1.0565.00.02018
\n","

284 rows × 6 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":165}]},{"cell_type":"code","source":["inflow_all.loc[(inflow_all.inmigrant_returns==1),'%inflow'] = 0"],"metadata":{"id":"bZdhZUP7x3w9","executionInfo":{"status":"ok","timestamp":1668725784248,"user_tz":-300,"elapsed":421,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":164,"outputs":[]},{"cell_type":"code","source":["inflow_all.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/inflow_all_final.csv', index=False)\n"],"metadata":{"id":"PAir5GvryG2w","executionInfo":{"status":"ok","timestamp":1668725797285,"user_tz":-300,"elapsed":955,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":166,"outputs":[]},{"cell_type":"code","source":["inflow_all.nunique()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"e93GcjpExlXb","executionInfo":{"status":"ok","timestamp":1668725249007,"user_tz":-300,"elapsed":430,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"b8552392-0453-41ed-cedd-60fc09d82582"},"execution_count":159,"outputs":[{"output_type":"execute_result","data":{"text/plain":["destination_countyfips 3158\n","county 8252\n","inmigrant_returns 11833\n","nonmigrant_returns 37619\n","%inflow 82496\n","Year 28\n","dtype: int64"]},"metadata":{},"execution_count":159}]},{"cell_type":"code","source":["28*3158"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"dRADbE8fxrZM","executionInfo":{"status":"ok","timestamp":1668725265894,"user_tz":-300,"elapsed":503,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"0cb1f139-2dfc-42d1-d320-d4c16b6dba10"},"execution_count":160,"outputs":[{"output_type":"execute_result","data":{"text/plain":["88424"]},"metadata":{},"execution_count":160}]},{"cell_type":"code","source":["inflow_all"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":554},"id":"OEnYH-URxDpg","executionInfo":{"status":"ok","timestamp":1668725218128,"user_tz":-300,"elapsed":359,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"a34880cf-078e-4aa5-e7c1-3339710262dd"},"execution_count":157,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" destination_countyfips \\\n","0 57001 \n","1 21117 \n","2 21119 \n","3 21121 \n","4 21123 \n","... ... \n","87740 40029 \n","87741 40027 \n","87742 40023 \n","87743 39147 \n","87744 1019 \n","\n"," county inmigrant_returns \\\n","0 Foreign / Overseas 35338.0 \n","1 Kenton 4540.0 \n","2 Knott 172.0 \n","3 Knox 419.0 \n","4 Larue 259.0 \n","... ... ... \n","87740 Coal County Total Migration-US and Foreign 144.0 \n","87741 Cleveland County Total Migration-US and Foreign 9779.0 \n","87742 Choctaw County Total Migration-US and Foreign 333.0 \n","87743 Seneca County Total Migration-US and Foreign 1181.0 \n","87744 Cherokee County Total Migration-US and Foreign 623.0 \n","\n"," nonmigrant_returns %inflow Year \n","0 151429.0 23.336349 1993 \n","1 48791.0 9.304995 1993 \n","2 3649.0 4.713620 1993 \n","3 6766.0 6.192728 1993 \n","4 3688.0 7.022777 1993 \n","... ... ... ... \n","87740 1531.0 9.405617 2020 \n","87741 93352.0 10.475405 2020 \n","87742 4219.0 7.892866 2020 \n","87743 21161.0 5.581022 2020 \n","87744 7614.0 8.182296 2020 \n","\n","[87745 rows x 6 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
destination_countyfipscountyinmigrant_returnsnonmigrant_returns%inflowYear
057001Foreign / Overseas35338.0151429.023.3363491993
121117Kenton4540.048791.09.3049951993
221119Knott172.03649.04.7136201993
321121Knox419.06766.06.1927281993
421123Larue259.03688.07.0227771993
.....................
8774040029Coal County Total Migration-US and Foreign144.01531.09.4056172020
8774140027Cleveland County Total Migration-US and Foreign9779.093352.010.4754052020
8774240023Choctaw County Total Migration-US and Foreign333.04219.07.8928662020
8774339147Seneca County Total Migration-US and Foreign1181.021161.05.5810222020
877441019Cherokee County Total Migration-US and Foreign623.07614.08.1822962020
\n","

87745 rows × 6 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":157}]},{"cell_type":"markdown","source":["# ETL Outtflow Files"],"metadata":{"id":"vIBXJ0htjRgW"}},{"cell_type":"code","source":["def read_xls_outflow(path_xls):\n"," \"\"\"read xls file, remove header rows, rename columns\"\"\"\n"," df = pd.read_excel(path_xls, engine='xlrd',skiprows=8, header=None , names=['origin_state','origin_county','destination_state', 'destination_county', \n"," 'destination_usps','county_names', 'numb_returns', 'numb_exemptions','agi'])\n"," return df\n","\n","\n","def set_fips(df):\n"," \"\"\"Calculate county fips using state and county area code \"\"\"\n"," cleaned_df = pd.DataFrame()\n"," cleaned_df['origin_countyfips'] = df.origin_state*1000 + df.origin_county\n"," cleaned_df['destination_countyfips'] = df.destination_state*1000 + df.destination_county\n"," cleaned_df['county'] = df.county_names\n"," cleaned_df['returns'] = df.numb_returns\n"," cleaned_df['exemptions'] = df.numb_exemptions\n"," return cleaned_df\n","\n","\n","def outflow_state(cleaned_df, format=1):\n"," \"\"\"Merge migrant and non migrant datasets to calculate %outflow.\"\"\"\n","\n"," if format==1:\n"," migrant = cleaned_df[cleaned_df.destination_countyfips == 1].reset_index(drop=True)\n"," migrant.rename(columns={'returns':'outmigrant_returns', 'exemptions':'outmigrant_exemptions'}, inplace=True)\n"," migrant.drop(columns=['destination_countyfips'], inplace=True)\n","\n"," nonmigrant = cleaned_df[cleaned_df.destination_countyfips==63050].reset_index(drop=True)\n"," nonmigrant.rename(columns={'returns':'nonmigrant_returns', 'exemptions':'nonmigrant_exemptions'}, inplace=True)\n"," nonmigrant.drop(columns=['destination_countyfips', 'county'], inplace=True)\n"," \n"," elif format==2:\n"," migrant = cleaned_df[cleaned_df.destination_countyfips==96000].reset_index(drop=True).drop(columns='destination_countyfips')\n"," migrant.rename(columns={'returns':'outmigrant_returns', 'exemptions':'outmigrant_exemptions'}, inplace=True)\n"," migrant = migrant[~(migrant.county=='Total Mig - US & For')].reset_index(drop=True)\n","\n"," nonmigrant = cleaned_df[cleaned_df.destination_countyfips==cleaned_df.origin_countyfips].reset_index(drop=True).drop(columns=['destination_countyfips','county'])\n"," nonmigrant.rename(columns={'returns':'nonmigrant_returns', 'exemptions':'nonmigrant_exemptions'}, inplace=True)\n","\n"," outflow = migrant.merge(nonmigrant, on='origin_countyfips', how='left')\n"," outflow = outflow[~(outflow.outmigrant_returns=='d') | ~(outflow.nonmigrant_returns=='d')].reset_index(drop=True)\n"," outflow.outmigrant_returns = outflow.outmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," outflow.nonmigrant_returns = outflow.nonmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," outflow['%outflow'] = 100*(outflow['outmigrant_returns']/outflow['nonmigrant_returns'])\n"," return outflow\n","\n","\n","def process_year_outflow(xls_files, format=1):\n","\n"," \"\"\"Process and merge all states for a given Year\"\"\"\n"," outflow_allstates = pd.DataFrame()\n"," for xls_file in xls_files:\n"," df = read_xls_outflow(xls_file)\n"," cleaned_df = set_fips(df)\n"," outflow_df = outflow_state(cleaned_df, format)\n"," outflow_allstates = outflow_allstates.append(outflow_df)\n"," return outflow_allstates"],"metadata":{"id":"9BIbUkwMnNC3","executionInfo":{"status":"ok","timestamp":1668695630426,"user_tz":-300,"elapsed":597,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":9,"outputs":[]},{"cell_type":"code","source":["import glob as glob"],"metadata":{"id":"YHiLJ8-LYskz","executionInfo":{"status":"ok","timestamp":1668719152394,"user_tz":-300,"elapsed":516,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":101,"outputs":[]},{"cell_type":"code","source":["unzipped_folders = glob.glob(\"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped/*/*Outflow\")\n","unzipped_folders = unzipped_folders[1:]"],"metadata":{"id":"lCuS2fcTjURk","executionInfo":{"status":"ok","timestamp":1668719153034,"user_tz":-300,"elapsed":2,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":102,"outputs":[]},{"cell_type":"markdown","source":["## Format 1 - (Year 1993-1995)"],"metadata":{"id":"LYC7Ei_XHhlM"}},{"cell_type":"code","source":["unzipped_folders9295 = unzipped_folders[0:3]\n","years = [1993,1994,1995]\n","i=0\n","outflow_9295 = pd.DataFrame()\n","for unzipped_folder in unzipped_folders9295:\n"," xls_files_i = glob.glob(unzipped_folder + \"/*.xls\")\n"," outflow_i = process_year_outflow(xls_files_i, format=1)\n"," outflow_i['Year'] = years[i]\n"," outflow_9295 = outflow_9295.append(outflow_i)\n"," i = i+1\n","outflow_9295 = outflow_9295.reset_index(drop=True)\n","outflow_9295 = outflow_9295[~(outflow_9295['nonmigrant_exemptions'].isna())].reset_index(drop=True)\n"],"metadata":{"id":"ZufaiLoD_Uel"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["outflow_9295.outmigrant_exemptions = outflow_9295.outmigrant_exemptions.astype('float64')\n","outflow_9295.nonmigrant_exemptions = outflow_9295.nonmigrant_exemptions.astype('float64')"],"metadata":{"id":"VFISok3ZAaRZ","executionInfo":{"status":"ok","timestamp":1668695745983,"user_tz":-300,"elapsed":475,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":11,"outputs":[]},{"cell_type":"code","source":["outflow_9295.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_inflow1993_1995.csv', index=False)"],"metadata":{"id":"uxv2Eg1T_I8J","executionInfo":{"status":"ok","timestamp":1668712264979,"user_tz":-300,"elapsed":567,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":49,"outputs":[]},{"cell_type":"code","source":["outflow_9295"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":574},"id":"yFeBWclsBGFl","executionInfo":{"status":"ok","timestamp":1668712004378,"user_tz":-300,"elapsed":976,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"a8cf964c-9a91-4b37-da5f-1db5981cc24f"},"execution_count":48,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" origin_countyfips county outmigrant_returns \\\n","0 57001 Foreign / Overseas 94415.0 \n","1 57003 Foreign / Puerto Rico 2556.0 \n","2 57005 APO / FPO Zip Code 141212.0 \n","3 57007 Foreign / Virgin Islands 1867.0 \n","4 56001 Albany 1437.0 \n","... ... ... ... \n","9398 1125 Tuscaloosa 2962.0 \n","9399 1127 Walker 1085.0 \n","9400 1129 Washington 264.0 \n","9401 1131 Wilcox 218.0 \n","9402 1133 Winston 492.0 \n","\n"," outmigrant_exemptions nonmigrant_returns nonmigrant_exemptions \\\n","0 125389.0 151429.0 310294.0 \n","1 6699.0 67648.0 124186.0 \n","2 316363.0 169479.0 411763.0 \n","3 5297.0 5102.0 9542.0 \n","4 2598.0 9384.0 20095.0 \n","... ... ... ... \n","9398 5519.0 46687.0 106604.0 \n","9399 2329.0 21419.0 50819.0 \n","9400 624.0 5051.0 12800.0 \n","9401 499.0 3566.0 9638.0 \n","9402 1041.0 7720.0 17958.0 \n","\n"," %outflow Year \n","0 62.349352 1993 \n","1 3.778382 1993 \n","2 83.321237 1993 \n","3 36.593493 1993 \n","4 15.313299 1993 \n","... ... ... \n","9398 6.344379 1995 \n","9399 5.065596 1995 \n","9400 5.226688 1995 \n","9401 6.113292 1995 \n","9402 6.373057 1995 \n","\n","[9403 rows x 8 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
origin_countyfipscountyoutmigrant_returnsoutmigrant_exemptionsnonmigrant_returnsnonmigrant_exemptions%outflowYear
057001Foreign / Overseas94415.0125389.0151429.0310294.062.3493521993
157003Foreign / Puerto Rico2556.06699.067648.0124186.03.7783821993
257005APO / FPO Zip Code141212.0316363.0169479.0411763.083.3212371993
357007Foreign / Virgin Islands1867.05297.05102.09542.036.5934931993
456001Albany1437.02598.09384.020095.015.3132991993
...........................
93981125Tuscaloosa2962.05519.046687.0106604.06.3443791995
93991127Walker1085.02329.021419.050819.05.0655961995
94001129Washington264.0624.05051.012800.05.2266881995
94011131Wilcox218.0499.03566.09638.06.1132921995
94021133Winston492.01041.07720.017958.06.3730571995
\n","

9403 rows × 8 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":48}]},{"cell_type":"code","source":["outflow_9295.describe()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":364},"id":"3p61eOz-BHgy","executionInfo":{"status":"ok","timestamp":1668695792017,"user_tz":-300,"elapsed":1052,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"e1285b5b-c230-4c8a-dda2-4f6a275c7f61"},"execution_count":13,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" origin_countyfips outmigrant_returns outmigrant_exemptions \\\n","count 9403.000000 9403.000000 9403.000000 \n","mean 30442.507072 2093.558332 4135.697437 \n","std 15174.830960 6420.878924 12629.277136 \n","min 1001.000000 13.000000 19.000000 \n","25% 18183.000000 236.000000 500.000000 \n","50% 29177.000000 520.000000 1063.000000 \n","75% 45090.000000 1347.500000 2709.000000 \n","max 57007.000000 160038.000000 334401.000000 \n","\n"," nonmigrant_returns nonmigrant_exemptions %outflow Year \n","count 9.403000e+03 9.403000e+03 9403.000000 9403.000000 \n","mean 2.734336e+04 6.130037e+04 7.951433 1994.000000 \n","std 8.812076e+04 1.949182e+05 3.861222 0.816497 \n","min 1.380000e+02 3.210000e+02 2.901948 1993.000000 \n","25% 3.192000e+03 7.650000e+03 6.122449 1993.000000 \n","50% 7.157000e+03 1.708100e+04 7.251908 1994.000000 \n","75% 1.822100e+04 4.273300e+04 8.829932 1995.000000 \n","max 2.782437e+06 6.437122e+06 102.862099 1995.000000 "],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
origin_countyfipsoutmigrant_returnsoutmigrant_exemptionsnonmigrant_returnsnonmigrant_exemptions%outflowYear
count9403.0000009403.0000009403.0000009.403000e+039.403000e+039403.0000009403.000000
mean30442.5070722093.5583324135.6974372.734336e+046.130037e+047.9514331994.000000
std15174.8309606420.87892412629.2771368.812076e+041.949182e+053.8612220.816497
min1001.00000013.00000019.0000001.380000e+023.210000e+022.9019481993.000000
25%18183.000000236.000000500.0000003.192000e+037.650000e+036.1224491993.000000
50%29177.000000520.0000001063.0000007.157000e+031.708100e+047.2519081994.000000
75%45090.0000001347.5000002709.0000001.822100e+044.273300e+048.8299321995.000000
max57007.000000160038.000000334401.0000002.782437e+066.437122e+06102.8620991995.000000
\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":13}]},{"cell_type":"code","source":["outflow_9295.info()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VpMrvheGBQ7n","executionInfo":{"status":"ok","timestamp":1668695799158,"user_tz":-300,"elapsed":655,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"8a53af28-5bcb-46a9-92d5-377c26c32f67"},"execution_count":14,"outputs":[{"output_type":"stream","name":"stdout","text":["\n","RangeIndex: 9403 entries, 0 to 9402\n","Data columns (total 8 columns):\n"," # Column Non-Null Count Dtype \n","--- ------ -------------- ----- \n"," 0 origin_countyfips 9403 non-null int64 \n"," 1 county 9401 non-null object \n"," 2 outmigrant_returns 9403 non-null float64\n"," 3 outmigrant_exemptions 9403 non-null float64\n"," 4 nonmigrant_returns 9403 non-null float64\n"," 5 nonmigrant_exemptions 9403 non-null float64\n"," 6 %outflow 9403 non-null float64\n"," 7 Year 9403 non-null int64 \n","dtypes: float64(5), int64(2), object(1)\n","memory usage: 587.8+ KB\n"]}]},{"cell_type":"code","source":["outflow_9295.nunique()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"oGLo5iDBBTBf","executionInfo":{"status":"ok","timestamp":1668695815998,"user_tz":-300,"elapsed":1021,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"4ea4ea82-ece4-48be-f6ba-53bfe4c52019"},"execution_count":15,"outputs":[{"output_type":"execute_result","data":{"text/plain":["origin_countyfips 3141\n","county 3680\n","outmigrant_returns 3212\n","outmigrant_exemptions 4332\n","nonmigrant_returns 7790\n","nonmigrant_exemptions 8635\n","%outflow 9289\n","Year 3\n","dtype: int64"]},"metadata":{},"execution_count":15}]},{"cell_type":"code","source":["import plotly.express as px\n","import matplotlib.pyplot as plt\n","from urllib.request import urlopen\n","import json\n","with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:\n"," counties = json.load(response)"],"metadata":{"id":"PpfbzYyhCj2p","executionInfo":{"status":"ok","timestamp":1668696557303,"user_tz":-300,"elapsed":5,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":22,"outputs":[]},{"cell_type":"code","source":["#@title Annual Migration outflow returns filed, 1993-1995\n","df = outflow_9295.copy()\n","df['County Code'] = df.origin_countyfips.astype('str').str.zfill(5)\n","fig = px.choropleth(df, locations=\"County Code\",\n"," geojson=counties,\n"," color=\"%outflow\",\n"," hover_name=\"county\",\n"," scope=\"usa\",\n"," animation_frame='Year',\n"," range_color=(0, 30),\n"," color_continuous_scale=\"ylorrd\")\n"," \n","fig.update_layout(showlegend=False, title='Annual Migration outflow returns filed, 1993-1995')\n","fig.update_traces(marker_line_width=0.25)\n","fig.show()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542,"output_embedded_package_id":"1kTffs9ZwRLcNbH790nA0SRsUGcJhO5O4"},"id":"FjzvN2qHBZsB","executionInfo":{"status":"ok","timestamp":1668697063635,"user_tz":-300,"elapsed":59541,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"79cc3594-d5d6-42d9-fb3d-c22a8279136b"},"execution_count":28,"outputs":[{"output_type":"display_data","data":{"text/plain":"Output hidden; open in https://colab.research.google.com to view."},"metadata":{}}]},{"cell_type":"code","source":["outflow_9295[outflow_9295.origin_countyfips==22115]"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":242},"id":"AWafckV1DKLC","executionInfo":{"status":"ok","timestamp":1668700590748,"user_tz":-300,"elapsed":777,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"913599ee-17a7-4838-bf19-0eb7441c16bf"},"execution_count":35,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" origin_countyfips county outmigrant_returns \\\n","2019 22115 Vernon 7697.0 \n","5154 22115 Vernon (Total Migrant) 5589.0 \n","8287 22115 Vernon 3921.0 \n","\n"," outmigrant_exemptions nonmigrant_returns nonmigrant_exemptions \\\n","2019 19748.0 12997.0 33497.0 \n","5154 14288.0 12093.0 30866.0 \n","8287 9440.0 13946.0 35785.0 \n","\n"," %outflow Year \n","2019 59.221359 1993 \n","5154 46.216820 1994 \n","8287 28.115589 1995 "],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
origin_countyfipscountyoutmigrant_returnsoutmigrant_exemptionsnonmigrant_returnsnonmigrant_exemptions%outflowYear
201922115Vernon7697.019748.012997.033497.059.2213591993
515422115Vernon (Total Migrant)5589.014288.012093.030866.046.2168201994
828722115Vernon3921.09440.013946.035785.028.1155891995
\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":35}]},{"cell_type":"code","source":["inflow_9295[inflow_9295.destination_countyfips==22115]"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"tm8lAxy_TkwJ","executionInfo":{"status":"ok","timestamp":1668700991632,"user_tz":-300,"elapsed":970,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"2eb595e9-c850-44b9-ad76-74feb4dcc753"},"execution_count":36,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" destination_countyfips county \\\n","2019 22115 Vernon \n","4303 22115 Vernon (Total Migrant) \n","7436 22115 Vernon \n","10568 22115 Vernon Parish Tot Mig-US & For \n","13699 22115 Vernon Parish Tot Mig-US & For \n","16825 22115 Vernon Parish Tot Mig-US & For \n","19953 22115 Vernon Parish Tot Mig-US & For \n","23083 22115 Vernon Parish Tot Mig-US & For \n","26218 22115 Vernon Parish Tot Mig-US & For \n","29348 22115 Vernon Parish Tot Mig-US & For \n","32479 22115 Vernon Parish Tot Mig-US & For \n","35610 22115 Vernon Parish Tot Mig-US & For \n","\n"," inmigrant_returns inmigrant_exemptions nonmigrant_returns \\\n","2019 4428.0 10247.0 12997.0 \n","4303 5537.0 13652.0 12093.0 \n","7436 3148.0 7407.0 13946.0 \n","10568 2847.0 6825.0 13742.0 \n","13699 2698.0 6182.0 14039.0 \n","16825 3139.0 7585.0 13336.0 \n","19953 3252.0 7381.0 14278.0 \n","23083 2992.0 6567.0 14077.0 \n","26218 3044.0 6771.0 14236.0 \n","29348 2938.0 6406.0 14417.0 \n","32479 3172.0 6959.0 14392.0 \n","35610 2531.0 5737.0 14194.0 \n","\n"," nonmigrant_exemptions %inflow Year \n","2019 33497.0 34.069401 1993 \n","4303 30866.0 45.786819 1994 \n","7436 35785.0 22.572781 1995 \n","10568 34471.0 20.717508 1998 \n","13699 35587.0 19.217893 1997 \n","16825 33847.0 23.537792 1996 \n","19953 34819.0 22.776299 2000 \n","23083 34806.0 21.254529 1999 \n","26218 34301.0 21.382411 2003 \n","29348 34749.0 20.378720 2002 \n","32479 35017.0 22.040022 2001 \n","35610 34601.0 17.831478 2004 "],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
destination_countyfipscountyinmigrant_returnsinmigrant_exemptionsnonmigrant_returnsnonmigrant_exemptions%inflowYear
201922115Vernon4428.010247.012997.033497.034.0694011993
430322115Vernon (Total Migrant)5537.013652.012093.030866.045.7868191994
743622115Vernon3148.07407.013946.035785.022.5727811995
1056822115Vernon Parish Tot Mig-US & For2847.06825.013742.034471.020.7175081998
1369922115Vernon Parish Tot Mig-US & For2698.06182.014039.035587.019.2178931997
1682522115Vernon Parish Tot Mig-US & For3139.07585.013336.033847.023.5377921996
1995322115Vernon Parish Tot Mig-US & For3252.07381.014278.034819.022.7762992000
2308322115Vernon Parish Tot Mig-US & For2992.06567.014077.034806.021.2545291999
2621822115Vernon Parish Tot Mig-US & For3044.06771.014236.034301.021.3824112003
2934822115Vernon Parish Tot Mig-US & For2938.06406.014417.034749.020.3787202002
3247922115Vernon Parish Tot Mig-US & For3172.06959.014392.035017.022.0400222001
3561022115Vernon Parish Tot Mig-US & For2531.05737.014194.034601.017.8314782004
\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":36}]},{"cell_type":"markdown","source":["## Format 2 (Year 1996-2004)"],"metadata":{"id":"Ml9VxBq86YuX"}},{"cell_type":"code","source":["unzipped_folder95onw = unzipped_folders[3:]\n","years = list(range(1996,2005))\n","i=0\n","outflow_on95 = pd.DataFrame()\n","for unzipped_folder in unzipped_folder95onw:\n"," xls_files_i = glob.glob(unzipped_folder + \"/*.xls\")\n"," outflow_i = process_year_outflow(xls_files_i, format=2)\n"," outflow_i['Year'] = years[i]\n"," outflow_on95 = outflow_on95.append(outflow_i)\n"," i = i+1\n","outflow_on95 = outflow_on95.reset_index(drop=True)\n","outflow_on95 = outflow_on95[~(outflow_on95['nonmigrant_exemptions'].isna())].reset_index(drop=True)\n","outflow_on95.outmigrant_exemptions = outflow_on95.outmigrant_exemptions.astype('float64')\n","outflow_on95.nonmigrant_exemptions = outflow_on95.nonmigrant_exemptions.astype('float64')"],"metadata":{"id":"fy6fbcQcVGha","executionInfo":{"status":"ok","timestamp":1668719489515,"user_tz":-300,"elapsed":68706,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":110,"outputs":[]},{"cell_type":"code","source":["#@title 1997 XLS files to read\n","\n","unzipped_folder97 = [unzipped_folders[4]]\n","years = [1997]\n","i=0\n","outflow_97 = pd.DataFrame()\n","for unzipped_folder in unzipped_folder97:\n"," xls_files_i = glob.glob(unzipped_folder + \"/*.XLS\")\n"," outflow_i = process_year_outflow(xls_files_i, format=2)\n"," outflow_i['Year'] = 1997\n"," outflow_97 = outflow_97.append(outflow_i)\n","outflow_97 = outflow_97.reset_index(drop=True)\n","outflow_97 = outflow_97[~(outflow_97['nonmigrant_exemptions'].isna())].reset_index(drop=True)\n","outflow_97.outmigrant_exemptions = outflow_97.outmigrant_exemptions.astype('float64')\n","outflow_97.nonmigrant_exemptions = outflow_97.nonmigrant_exemptions.astype('float64')\n"],"metadata":{"id":"MzsdILfTeSVw","executionInfo":{"status":"ok","timestamp":1668720422030,"user_tz":-300,"elapsed":10173,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":121,"outputs":[]},{"cell_type":"code","source":["outflow_on95 = outflow_97.append(outflow_on95).reset_index(drop=True)\n","outflow_on95 = outflow_on95.sort_values(by='Year').reset_index(drop=True)"],"metadata":{"id":"9kmd-VEbe9zG","executionInfo":{"status":"ok","timestamp":1668720506659,"user_tz":-300,"elapsed":415,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":123,"outputs":[]},{"cell_type":"code","source":["outflow_on95.info()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"JBvY54Zxbw6U","executionInfo":{"status":"ok","timestamp":1668720528520,"user_tz":-300,"elapsed":590,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"22614c84-a34a-43d8-9d5c-d1e0611f8855"},"execution_count":124,"outputs":[{"output_type":"stream","name":"stdout","text":["\n","RangeIndex: 28256 entries, 0 to 28255\n","Data columns (total 8 columns):\n"," # Column Non-Null Count Dtype \n","--- ------ -------------- ----- \n"," 0 origin_countyfips 28256 non-null int64 \n"," 1 county 28256 non-null object \n"," 2 outmigrant_returns 28256 non-null float64\n"," 3 outmigrant_exemptions 28256 non-null float64\n"," 4 nonmigrant_returns 28256 non-null float64\n"," 5 nonmigrant_exemptions 28256 non-null float64\n"," 6 %outflow 28256 non-null float64\n"," 7 Year 28256 non-null int64 \n","dtypes: float64(5), int64(2), object(1)\n","memory usage: 1.7+ MB\n"]}]},{"cell_type":"code","source":["outflow_on95.nunique()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"j8Vzxy7n6i-h","executionInfo":{"status":"ok","timestamp":1668720553294,"user_tz":-300,"elapsed":460,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"a712a18d-6596-4ce6-dcde-ba471722e947"},"execution_count":125,"outputs":[{"output_type":"execute_result","data":{"text/plain":["origin_countyfips 3139\n","county 2487\n","outmigrant_returns 6169\n","outmigrant_exemptions 8239\n","nonmigrant_returns 18221\n","nonmigrant_exemptions 22545\n","%outflow 27346\n","Year 9\n","dtype: int64"]},"metadata":{},"execution_count":125}]},{"cell_type":"code","source":["outflow_on95.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/outflow_19962004.csv', index=False)"],"metadata":{"id":"jSCadaUIb6Vz","executionInfo":{"status":"ok","timestamp":1668720585960,"user_tz":-300,"elapsed":565,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":126,"outputs":[]},{"cell_type":"code","source":["outflow_922004 = outflow_9295.append(outflow_on95).reset_index(drop=True)\n","outflow_922004 = outflow_922004.sort_values(by='Year').reset_index(drop=True)\n","outflow_922004.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/outflow_19932004.csv', index=False)"],"metadata":{"id":"ial6-VWafBOR","executionInfo":{"status":"ok","timestamp":1668720886378,"user_tz":-300,"elapsed":934,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":127,"outputs":[]},{"cell_type":"markdown","source":["## Format 3 Dat Files (Year 2005-2011)"],"metadata":{"id":"D4KexWzthNZc"}},{"cell_type":"code","source":["unzipped_folders = glob.glob(\"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyout*.dat\")\n","unzipped_folders"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Q9ecTvlZg_oc","executionInfo":{"status":"ok","timestamp":1668721186932,"user_tz":-300,"elapsed":440,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"cb2f66d6-419c-4660-a661-8cb3cbed9105"},"execution_count":128,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyout0405us1.dat',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyout0506.dat',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyout0607.dat',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyoutflow0809.dat',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyoutflow0910.dat',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/countyoutflow1011.dat']"]},"metadata":{},"execution_count":128}]},{"cell_type":"code","source":["years = [2005,2006,2007,2009,2010,2011]\n","i=0\n","outflow_2004on = pd.DataFrame()\n","for unzip_dat in unzipped_folders:\n"," dat_data = pd.read_fwf(unzip_dat, header=None,encoding = \"ISO-8859-1\", names=['origin_state','origin_county','destination_state', 'destination_county', \n"," 'destination_usps','county_names', 'numb_returns', 'numb_exemptions','agi', 'unknown'])\n"," dat_data.origin_countyfips = dat_data.origin_state*1000 + dat_data.origin_county\n"," dat_data.destination_countyfips = dat_data.destination_state*1000 + dat_data.destination_county\n"," cleaned_df = set_fips(dat_data)\n"," process_df = outflow_state(cleaned_df, format=2)\n"," process_df['Year'] = years[i]\n"," i=i+1\n"," outflow_2004on = outflow_2004on.append(process_df)\n","outflow_2004on = outflow_2004on.reset_index(drop=True)\n","outflow_2004on.nunique()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"4Qbp5Zwbi1sf","executionInfo":{"status":"ok","timestamp":1668721665513,"user_tz":-300,"elapsed":15270,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"8ab4e406-3d69-4517-cb91-39a7d43740bc"},"execution_count":130,"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:7: UserWarning:\n","\n","Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access\n","\n","/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning:\n","\n","Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access\n","\n"]},{"output_type":"execute_result","data":{"text/plain":["origin_countyfips 3146\n","county 1935\n","outmigrant_returns 5092\n","outmigrant_exemptions 6705\n","nonmigrant_returns 13922\n","nonmigrant_exemptions 16188\n","%outflow 18387\n","Year 6\n","dtype: int64"]},"metadata":{},"execution_count":130}]},{"cell_type":"code","source":["3146*6"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Vaqxy8fYkCpa","executionInfo":{"status":"ok","timestamp":1668721688797,"user_tz":-300,"elapsed":482,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"ee61c6cb-e1be-4106-dbfc-b4c25f9a3b95"},"execution_count":132,"outputs":[{"output_type":"execute_result","data":{"text/plain":["18876"]},"metadata":{},"execution_count":132}]},{"cell_type":"code","source":["outflow_2004on.info()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VJSUETcAj6fP","executionInfo":{"status":"ok","timestamp":1668721679398,"user_tz":-300,"elapsed":569,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"95717591-220b-4347-e2b1-539d126ecb66"},"execution_count":131,"outputs":[{"output_type":"stream","name":"stdout","text":["\n","RangeIndex: 18856 entries, 0 to 18855\n","Data columns (total 8 columns):\n"," # Column Non-Null Count Dtype \n","--- ------ -------------- ----- \n"," 0 origin_countyfips 18856 non-null int64 \n"," 1 county 18856 non-null object \n"," 2 outmigrant_returns 18856 non-null float64\n"," 3 outmigrant_exemptions 18856 non-null int64 \n"," 4 nonmigrant_returns 18856 non-null float64\n"," 5 nonmigrant_exemptions 18856 non-null int64 \n"," 6 %outflow 18856 non-null float64\n"," 7 Year 18856 non-null int64 \n","dtypes: float64(3), int64(4), object(1)\n","memory usage: 1.2+ MB\n"]}]},{"cell_type":"code","source":["#@title 2008 Dat file\n","outflow_2008 = pd.DataFrame()\n","unzip_dat = '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_dat/co0708us.dat'\n","dat_data = pd.read_fwf(unzip_dat, header=None,encoding = \"ISO-8859-1\", names=['origin_state','origin_county','destination_state', 'destination_county', \n"," 'destination_usps','county_names', 'numb_returns', 'numb_exemptions','agi', 'unknown'])\n","dat_data.origin_countyfips = dat_data.origin_state*1000 + dat_data.origin_county\n","dat_data.destination_countyfips = dat_data.destination_state*1000 + dat_data.destination_county\n","cleaned_df = set_fips(dat_data)\n","process_df = outflow_state(cleaned_df, format=2)\n","process_df['Year'] = 2008\n","outflow_2008 = outflow_2008.append(process_df)\n","outflow_2008 = outflow_2008.reset_index(drop=True)\n","outflow_2008.nunique()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"IBElS2NFkBb8","executionInfo":{"status":"ok","timestamp":1668722337375,"user_tz":-300,"elapsed":2851,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"6d04427c-e52e-4175-e19b-0637cc14f46c"},"execution_count":135,"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: UserWarning:\n","\n","Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access\n","\n","/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:7: UserWarning:\n","\n","Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access\n","\n"]},{"output_type":"execute_result","data":{"text/plain":["origin_countyfips 3142\n","county 1875\n","outmigrant_returns 1834\n","outmigrant_exemptions 2240\n","nonmigrant_returns 2976\n","nonmigrant_exemptions 3056\n","%outflow 3116\n","Year 1\n","dtype: int64"]},"metadata":{},"execution_count":135}]},{"cell_type":"code","source":["outflow_2008.info()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"4UAtFaV4mGew","executionInfo":{"status":"ok","timestamp":1668722358333,"user_tz":-300,"elapsed":534,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"b165b836-f728-4063-9136-510f12c03e9e"},"execution_count":136,"outputs":[{"output_type":"stream","name":"stdout","text":["\n","RangeIndex: 3142 entries, 0 to 3141\n","Data columns (total 8 columns):\n"," # Column Non-Null Count Dtype \n","--- ------ -------------- ----- \n"," 0 origin_countyfips 3142 non-null int64 \n"," 1 county 3142 non-null object \n"," 2 outmigrant_returns 3142 non-null float64\n"," 3 outmigrant_exemptions 3142 non-null int64 \n"," 4 nonmigrant_returns 3142 non-null float64\n"," 5 nonmigrant_exemptions 3142 non-null int64 \n"," 6 %outflow 3142 non-null float64\n"," 7 Year 3142 non-null int64 \n","dtypes: float64(3), int64(4), object(1)\n","memory usage: 196.5+ KB\n"]}]},{"cell_type":"code","source":["outflow_1992_2011 = outflow_922004.append(outflow_2004on).append(outflow_2008).reset_index(drop=True)\n","outflow_1992_2011.sort_values(by='Year').reset_index(drop=True)\n","outflow_1992_2011.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/outflow_1992_2011.csv', index=False)"],"metadata":{"id":"rBw4L9VxmnLP","executionInfo":{"status":"ok","timestamp":1668722494820,"user_tz":-300,"elapsed":953,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":137,"outputs":[]},{"cell_type":"markdown","source":["## Format 4 (Year 2012-2020)"],"metadata":{"id":"jtPzSoS6nsGr"}},{"cell_type":"code","source":["unzipped_folders = glob.glob(\"/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow*.csv\")\n","unzipped_folders"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"2bIp-b2hnIds","executionInfo":{"status":"ok","timestamp":1668722679788,"user_tz":-300,"elapsed":639,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"aa81b51b-c3bc-4081-fcd4-e6bfe3103341"},"execution_count":138,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1112.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1314.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1213.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1415.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1516.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1617.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1718.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1819.csv',\n"," '/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/migration_raw/unzipped_2012_2021/countyoutflow1920.csv']"]},"metadata":{},"execution_count":138}]},{"cell_type":"code","source":["years = [2012,2014,2013,2015,2016,2017,2018,2019,2020]\n","i=0\n","outflow_1221 = pd.DataFrame()\n","for csv_path in unzipped_folders:\n"," df = pd.read_csv(csv_path, encoding = \"ISO-8859-1\")\n"," cleaned_df = pd.DataFrame()\n"," cleaned_df['origin_countyfips'] = df.y1_statefips*1000 + df.y1_countyfips\n"," cleaned_df['destination_countyfips'] = df.y2_statefips*1000 + df.y2_countyfips\n"," cleaned_df['county'] = df.y2_countyname\n"," cleaned_df['returns'] = df.n1\n"," cleaned_df['individuals'] = df.n2\n"," migrant = cleaned_df[cleaned_df.destination_countyfips==96000].reset_index(drop=True).drop(columns='destination_countyfips')\n"," migrant.rename(columns={'returns':'outmigrant_returns', 'individuals':'outmigrants'}, inplace=True)\n"," migrant = migrant[~(migrant.county == 'Total Migration-US and Foreign')].reset_index(drop=True)\n"," nonmigrant = cleaned_df[cleaned_df.destination_countyfips==cleaned_df.origin_countyfips].reset_index(drop=True).drop(columns=['destination_countyfips','county'])\n"," nonmigrant.rename(columns={'returns':'nonmigrant_returns', 'individuals':'nonmigrants'}, inplace=True)\n"," outflow = migrant.merge(nonmigrant, on='origin_countyfips', how='left')\n"," outflow = outflow[~(outflow.outmigrant_returns=='d') | ~(outflow.nonmigrant_returns=='d')].reset_index(drop=True)\n"," outflow.outmigrant_returns = outflow.outmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," outflow.nonmigrant_returns = outflow.nonmigrant_returns.astype('str').str.extract('(\\d+)').astype('float')\n"," outflow['%outflow'] = 100*(outflow['outmigrant_returns']/outflow['nonmigrant_returns'])\n"," outflow['Year'] = years[i]\n"," outflow_1221 = outflow_1221.append(outflow)\n"," i=i+1\n","\n","outflow_1221.reset_index(drop=True)\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"8RSI6sFxn1rT","executionInfo":{"status":"ok","timestamp":1668723166617,"user_tz":-300,"elapsed":5054,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"c448286f-cedf-4f21-c559-046530a248c7"},"execution_count":141,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" origin_countyfips county \\\n","0 1001 Autauga County Total Migration-US and Foreign \n","1 1003 Baldwin County Total Migration-US and Foreign \n","2 1005 Barbour County Total Migration-US and Foreign \n","3 1007 Bibb County Total Migration-US and Foreign \n","4 1009 Blount County Total Migration-US and Foreign \n","... ... ... \n","28193 56037 Sweetwater County Total Migration-US and Foreign \n","28194 56039 Teton County Total Migration-US and Foreign \n","28195 56041 Uinta County Total Migration-US and Foreign \n","28196 56043 Washakie County Total Migration-US and Foreign \n","28197 56045 Weston County Total Migration-US and Foreign \n","\n"," outmigrant_returns outmigrants nonmigrant_returns nonmigrants \\\n","0 2125.0 4776 17696.0 41757.0 \n","1 4327.0 8728 65215.0 144258.0 \n","2 771.0 1599 8001.0 17972.0 \n","3 523.0 1184 6198.0 14840.0 \n","4 1252.0 2808 17390.0 42898.0 \n","... ... ... ... ... \n","28193 1084.0 2306 15424.0 35193.0 \n","28194 1152.0 1867 10840.0 20640.0 \n","28195 476.0 1037 7102.0 17922.0 \n","28196 171.0 306 2885.0 6592.0 \n","28197 186.0 343 2452.0 5267.0 \n","\n"," %outflow Year \n","0 12.008363 2012 \n","1 6.634977 2012 \n","2 9.636295 2012 \n","3 8.438206 2012 \n","4 7.199540 2012 \n","... ... ... \n","28193 7.028008 2020 \n","28194 10.627306 2020 \n","28195 6.702337 2020 \n","28196 5.927210 2020 \n","28197 7.585644 2020 \n","\n","[28198 rows x 8 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
origin_countyfipscountyoutmigrant_returnsoutmigrantsnonmigrant_returnsnonmigrants%outflowYear
01001Autauga County Total Migration-US and Foreign2125.0477617696.041757.012.0083632012
11003Baldwin County Total Migration-US and Foreign4327.0872865215.0144258.06.6349772012
21005Barbour County Total Migration-US and Foreign771.015998001.017972.09.6362952012
31007Bibb County Total Migration-US and Foreign523.011846198.014840.08.4382062012
41009Blount County Total Migration-US and Foreign1252.0280817390.042898.07.1995402012
...........................
2819356037Sweetwater County Total Migration-US and Foreign1084.0230615424.035193.07.0280082020
2819456039Teton County Total Migration-US and Foreign1152.0186710840.020640.010.6273062020
2819556041Uinta County Total Migration-US and Foreign476.010377102.017922.06.7023372020
2819656043Washakie County Total Migration-US and Foreign171.03062885.06592.05.9272102020
2819756045Weston County Total Migration-US and Foreign186.03432452.05267.07.5856442020
\n","

28198 rows × 8 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":141}]},{"cell_type":"code","source":["outflow_all = outflow_1992_2011.drop(columns=['outmigrant_exemptions', 'nonmigrant_exemptions']).append(outflow_1221.drop(columns=['outmigrants', 'nonmigrants']))\n","outflow_all\n","outflow_all.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/outflow_all.csv', index=False)\n","outflow_1221.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/outflow_1221.csv', index=False)"],"metadata":{"id":"vTMny-Z6pIJU","executionInfo":{"status":"ok","timestamp":1668723196394,"user_tz":-300,"elapsed":2039,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":142,"outputs":[]},{"cell_type":"code","source":["#@title valid return value starts from 10 as per meta file. Filtering some of the data points per that criterian\n","outflow_all[(outflow_all.outmigrant_returns==1)]['%outflow'] = 0"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"v8rwqGnCru_F","executionInfo":{"status":"ok","timestamp":1668723852571,"user_tz":-300,"elapsed":364,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"f70e9f4d-f71e-438e-df9f-f66099c67c9c"},"execution_count":146,"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: SettingWithCopyWarning:\n","\n","\n","A value is trying to be set on a copy of a slice from a DataFrame.\n","Try using .loc[row_indexer,col_indexer] = value instead\n","\n","See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n","\n"]}]},{"cell_type":"code","source":["outflow_all[outflow_all.outmigrant_returns==1]"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":554},"id":"Wjiktn5yr42r","executionInfo":{"status":"ok","timestamp":1668723857689,"user_tz":-300,"elapsed":364,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}},"outputId":"b34198bd-1bfa-47ce-fbac-2d619f167608"},"execution_count":147,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" origin_countyfips county \\\n","38205 15005 Kalawao County Tot Mig-US & For \n","39289 30069 Petroleum Count Tot Mig-US & Fo \n","39306 30103 Treasure County Tot Mig-US & Fo \n","39313 31005 Arthur County Tot Mig-US & For \n","39315 31009 Blaine County Tot Mig-US & For \n","... ... ... \n","2391 46063 Harding County Total Migration-US and Foreign \n","2394 46069 Hyde County Total Migration-US and Foreign \n","2652 48261 Kenedy County Total Migration-US and Foreign \n","2656 48269 King County Total Migration-US and Foreign \n","2672 48301 Loving County Total Migration-US and Foreign \n","\n"," outmigrant_returns nonmigrant_returns %outflow Year \n","38205 1.0 1.0 0.0 2005 \n","39289 1.0 1.0 0.0 2005 \n","39306 1.0 1.0 0.0 2005 \n","39313 1.0 1.0 0.0 2005 \n","39315 1.0 1.0 0.0 2005 \n","... ... ... ... ... \n","2391 1.0 503.0 0.0 2018 \n","2394 1.0 518.0 0.0 2018 \n","2652 1.0 111.0 0.0 2018 \n","2656 1.0 89.0 0.0 2018 \n","2672 1.0 28.0 0.0 2018 \n","\n","[231 rows x 6 columns]"],"text/html":["\n","
\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
origin_countyfipscountyoutmigrant_returnsnonmigrant_returns%outflowYear
3820515005Kalawao County Tot Mig-US & For1.01.00.02005
3928930069Petroleum Count Tot Mig-US & Fo1.01.00.02005
3930630103Treasure County Tot Mig-US & Fo1.01.00.02005
3931331005Arthur County Tot Mig-US & For1.01.00.02005
3931531009Blaine County Tot Mig-US & For1.01.00.02005
.....................
239146063Harding County Total Migration-US and Foreign1.0503.00.02018
239446069Hyde County Total Migration-US and Foreign1.0518.00.02018
265248261Kenedy County Total Migration-US and Foreign1.0111.00.02018
265648269King County Total Migration-US and Foreign1.089.00.02018
267248301Loving County Total Migration-US and Foreign1.028.00.02018
\n","

231 rows × 6 columns

\n","
\n"," \n"," \n"," \n","\n"," \n","
\n","
\n"," "]},"metadata":{},"execution_count":147}]},{"cell_type":"code","source":["outflow_all.to_csv('/content/gdrive/MyDrive/Data/Research Data/07Research/Research Papers/Hypothesis developed from research papers/outflow_all.csv', index=False)\n"],"metadata":{"id":"bpPiybIvsLp2","executionInfo":{"status":"ok","timestamp":1668723963132,"user_tz":-300,"elapsed":954,"user":{"displayName":"Sabaina Haroon","userId":"03287014315776100364"}}},"execution_count":148,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"Gho6aA9Ssu5C"},"execution_count":null,"outputs":[]}]}