{"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","
destination_countyfips
\n","
inmigrant_returns
\n","
inmigrant_exemptions
\n","
nonmigrant_returns
\n","
nonmigrant_exemptions
\n","
%inflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
count
\n","
56426.000000
\n","
56426.000000
\n","
56426.000000
\n","
5.642600e+04
\n","
5.642600e+04
\n","
56426.000000
\n","
56426.000000
\n","
\n","
\n","
mean
\n","
30403.395509
\n","
2215.937103
\n","
4206.826764
\n","
3.032793e+04
\n","
6.679927e+04
\n","
7.903940
\n","
2001.674175
\n","
\n","
\n","
std
\n","
15162.050856
\n","
5761.534846
\n","
10322.898247
\n","
9.692266e+04
\n","
2.119741e+05
\n","
4.383086
\n","
5.439342
\n","
\n","
\n","
min
\n","
1001.000000
\n","
1.000000
\n","
-1.000000
\n","
1.000000e+00
\n","
-1.000000e+00
\n","
1.780415
\n","
1993.000000
\n","
\n","
\n","
25%
\n","
18179.000000
\n","
244.000000
\n","
518.000000
\n","
3.403000e+03
\n","
7.934000e+03
\n","
5.722452
\n","
1997.000000
\n","
\n","
\n","
50%
\n","
29175.000000
\n","
563.000000
\n","
1166.000000
\n","
7.765000e+03
\n","
1.790950e+04
\n","
7.096774
\n","
2002.000000
\n","
\n","
\n","
75%
\n","
45083.000000
\n","
1543.000000
\n","
3082.000000
\n","
2.036775e+04
\n","
4.606325e+04
\n","
9.045557
\n","
2006.000000
\n","
\n","
\n","
max
\n","
57007.000000
\n","
110030.000000
\n","
193646.000000
\n","
3.290970e+06
\n","
7.244412e+06
\n","
100.000000
\n","
2011.000000
\n","
\n"," \n","
\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","
destination_countyfips
\n","
county
\n","
inmigrant_returns
\n","
inmigrants
\n","
nonmigrant_returns
\n","
nonmigrants
\n","
%inflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
0
\n","
1001
\n","
Autauga County Total Migration-US and Foreign
\n","
2006.0
\n","
4618
\n","
17696.0
\n","
41757.0
\n","
11.335895
\n","
2012
\n","
\n","
\n","
1
\n","
1003
\n","
Baldwin County Total Migration-US and Foreign
\n","
5633.0
\n","
11518
\n","
65215.0
\n","
144258.0
\n","
8.637583
\n","
2012
\n","
\n","
\n","
2
\n","
1005
\n","
Barbour County Total Migration-US and Foreign
\n","
665.0
\n","
1440
\n","
8001.0
\n","
17972.0
\n","
8.311461
\n","
2012
\n","
\n","
\n","
3
\n","
1007
\n","
Bibb County Total Migration-US and Foreign
\n","
479.0
\n","
1062
\n","
6198.0
\n","
14840.0
\n","
7.728299
\n","
2012
\n","
\n","
\n","
4
\n","
1009
\n","
Blount County Total Migration-US and Foreign
\n","
1257.0
\n","
2767
\n","
17390.0
\n","
42898.0
\n","
7.228292
\n","
2012
\n","
\n","
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
\n","
\n","
28172
\n","
56037
\n","
Sweetwater County Total Migration-US and Foreign
\n","
833.0
\n","
1673
\n","
15424.0
\n","
35193.0
\n","
5.400674
\n","
2020
\n","
\n","
\n","
28173
\n","
56039
\n","
Teton County Total Migration-US and Foreign
\n","
1275.0
\n","
1981
\n","
10840.0
\n","
20640.0
\n","
11.761993
\n","
2020
\n","
\n","
\n","
28174
\n","
56041
\n","
Uinta County Total Migration-US and Foreign
\n","
475.0
\n","
1083
\n","
7102.0
\n","
17922.0
\n","
6.688257
\n","
2020
\n","
\n","
\n","
28175
\n","
56043
\n","
Washakie County Total Migration-US and Foreign
\n","
205.0
\n","
396
\n","
2885.0
\n","
6592.0
\n","
7.105719
\n","
2020
\n","
\n","
\n","
28176
\n","
56045
\n","
Weston County Total Migration-US and Foreign
\n","
160.0
\n","
326
\n","
2452.0
\n","
5267.0
\n","
6.525285
\n","
2020
\n","
\n"," \n","
\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","
destination_countyfips
\n","
county
\n","
inmigrant_returns
\n","
nonmigrant_returns
\n","
%inflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
0
\n","
57001
\n","
Foreign / Overseas
\n","
35338.0
\n","
151429.0
\n","
23.336349
\n","
1993
\n","
\n","
\n","
1
\n","
57003
\n","
Foreign / Puerto Rico
\n","
6691.0
\n","
67648.0
\n","
9.890906
\n","
1993
\n","
\n","
\n","
2
\n","
57005
\n","
APO / FPO Zip Code
\n","
94040.0
\n","
169479.0
\n","
55.487701
\n","
1993
\n","
\n","
\n","
3
\n","
57007
\n","
Foreign / Virgin Islands
\n","
3211.0
\n","
5102.0
\n","
62.936103
\n","
1993
\n","
\n","
\n","
4
\n","
56001
\n","
Albany
\n","
1561.0
\n","
9384.0
\n","
16.634697
\n","
1993
\n","
\n","
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
\n","
\n","
3097
\n","
56037
\n","
Sweetwater County Total Migration-US and Foreign
\n","
833.0
\n","
15424.0
\n","
5.400674
\n","
2020
\n","
\n","
\n","
3098
\n","
56039
\n","
Teton County Total Migration-US and Foreign
\n","
1275.0
\n","
10840.0
\n","
11.761993
\n","
2020
\n","
\n","
\n","
3099
\n","
56041
\n","
Uinta County Total Migration-US and Foreign
\n","
475.0
\n","
7102.0
\n","
6.688257
\n","
2020
\n","
\n","
\n","
3100
\n","
56043
\n","
Washakie County Total Migration-US and Foreign
\n","
205.0
\n","
2885.0
\n","
7.105719
\n","
2020
\n","
\n","
\n","
3101
\n","
56045
\n","
Weston County Total Migration-US and Foreign
\n","
160.0
\n","
2452.0
\n","
6.525285
\n","
2020
\n","
\n"," \n","
\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","
destination_countyfips
\n","
county
\n","
inmigrant_returns
\n","
nonmigrant_returns
\n","
%inflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
0
\n","
57001
\n","
Foreign / Overseas
\n","
35338.0
\n","
151429.0
\n","
23.336349
\n","
1993
\n","
\n","
\n","
1
\n","
57003
\n","
Foreign / Puerto Rico
\n","
6691.0
\n","
67648.0
\n","
9.890906
\n","
1993
\n","
\n","
\n","
2
\n","
57005
\n","
APO / FPO Zip Code
\n","
94040.0
\n","
169479.0
\n","
55.487701
\n","
1993
\n","
\n","
\n","
3
\n","
57007
\n","
Foreign / Virgin Islands
\n","
3211.0
\n","
5102.0
\n","
62.936103
\n","
1993
\n","
\n","
\n","
4
\n","
56001
\n","
Albany
\n","
1561.0
\n","
9384.0
\n","
16.634697
\n","
1993
\n","
\n","
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
\n","
\n","
84598
\n","
56037
\n","
Sweetwater County Total Migration-US and Foreign
\n","
833.0
\n","
15424.0
\n","
5.400674
\n","
2020
\n","
\n","
\n","
84599
\n","
56039
\n","
Teton County Total Migration-US and Foreign
\n","
1275.0
\n","
10840.0
\n","
11.761993
\n","
2020
\n","
\n","
\n","
84600
\n","
56041
\n","
Uinta County Total Migration-US and Foreign
\n","
475.0
\n","
7102.0
\n","
6.688257
\n","
2020
\n","
\n","
\n","
84601
\n","
56043
\n","
Washakie County Total Migration-US and Foreign
\n","
205.0
\n","
2885.0
\n","
7.105719
\n","
2020
\n","
\n","
\n","
84602
\n","
56045
\n","
Weston County Total Migration-US and Foreign
\n","
160.0
\n","
2452.0
\n","
6.525285
\n","
2020
\n","
\n"," \n","
\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","
destination_countyfips
\n","
county
\n","
inmigrant_returns
\n","
nonmigrant_returns
\n","
%inflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
37759
\n","
38087
\n","
Slope County Tot Mig-US & For
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
38110
\n","
30103
\n","
Treasure County Tot Mig-US & Fo
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
38117
\n","
31005
\n","
Arthur County Tot Mig-US & For
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
38120
\n","
31009
\n","
Blaine County Tot Mig-US & For
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
38142
\n","
30069
\n","
Petroleum Count Tot Mig-US & Fo
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
\n","
\n","
80737
\n","
8017
\n","
Cheyenne County Total Migration-US and Foreign
\n","
1.0
\n","
635.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
80755
\n","
8053
\n","
Hinsdale County Total Migration-US and Foreign
\n","
1.0
\n","
291.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
81061
\n","
28055
\n","
Issaquena County Total Migration-US and Foreign
\n","
1.0
\n","
324.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
81184
\n","
20071
\n","
Greeley County Total Migration-US and Foreign
\n","
1.0
\n","
482.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
81220
\n","
20199
\n","
Wallace County Total Migration-US and Foreign
\n","
1.0
\n","
565.0
\n","
0.0
\n","
2018
\n","
\n"," \n","
\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"," "]},"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","
destination_countyfips
\n","
county
\n","
inmigrant_returns
\n","
inmigrant_exemptions
\n","
nonmigrant_returns
\n","
nonmigrant_exemptions
\n","
%inflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
2019
\n","
22115
\n","
Vernon
\n","
4428.0
\n","
10247.0
\n","
12997.0
\n","
33497.0
\n","
34.069401
\n","
1993
\n","
\n","
\n","
4303
\n","
22115
\n","
Vernon (Total Migrant)
\n","
5537.0
\n","
13652.0
\n","
12093.0
\n","
30866.0
\n","
45.786819
\n","
1994
\n","
\n","
\n","
7436
\n","
22115
\n","
Vernon
\n","
3148.0
\n","
7407.0
\n","
13946.0
\n","
35785.0
\n","
22.572781
\n","
1995
\n","
\n","
\n","
10568
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
2847.0
\n","
6825.0
\n","
13742.0
\n","
34471.0
\n","
20.717508
\n","
1998
\n","
\n","
\n","
13699
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
2698.0
\n","
6182.0
\n","
14039.0
\n","
35587.0
\n","
19.217893
\n","
1997
\n","
\n","
\n","
16825
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
3139.0
\n","
7585.0
\n","
13336.0
\n","
33847.0
\n","
23.537792
\n","
1996
\n","
\n","
\n","
19953
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
3252.0
\n","
7381.0
\n","
14278.0
\n","
34819.0
\n","
22.776299
\n","
2000
\n","
\n","
\n","
23083
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
2992.0
\n","
6567.0
\n","
14077.0
\n","
34806.0
\n","
21.254529
\n","
1999
\n","
\n","
\n","
26218
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
3044.0
\n","
6771.0
\n","
14236.0
\n","
34301.0
\n","
21.382411
\n","
2003
\n","
\n","
\n","
29348
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
2938.0
\n","
6406.0
\n","
14417.0
\n","
34749.0
\n","
20.378720
\n","
2002
\n","
\n","
\n","
32479
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
3172.0
\n","
6959.0
\n","
14392.0
\n","
35017.0
\n","
22.040022
\n","
2001
\n","
\n","
\n","
35610
\n","
22115
\n","
Vernon Parish Tot Mig-US & For
\n","
2531.0
\n","
5737.0
\n","
14194.0
\n","
34601.0
\n","
17.831478
\n","
2004
\n","
\n"," \n","
\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","
origin_countyfips
\n","
county
\n","
outmigrant_returns
\n","
outmigrants
\n","
nonmigrant_returns
\n","
nonmigrants
\n","
%outflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
0
\n","
1001
\n","
Autauga County Total Migration-US and Foreign
\n","
2125.0
\n","
4776
\n","
17696.0
\n","
41757.0
\n","
12.008363
\n","
2012
\n","
\n","
\n","
1
\n","
1003
\n","
Baldwin County Total Migration-US and Foreign
\n","
4327.0
\n","
8728
\n","
65215.0
\n","
144258.0
\n","
6.634977
\n","
2012
\n","
\n","
\n","
2
\n","
1005
\n","
Barbour County Total Migration-US and Foreign
\n","
771.0
\n","
1599
\n","
8001.0
\n","
17972.0
\n","
9.636295
\n","
2012
\n","
\n","
\n","
3
\n","
1007
\n","
Bibb County Total Migration-US and Foreign
\n","
523.0
\n","
1184
\n","
6198.0
\n","
14840.0
\n","
8.438206
\n","
2012
\n","
\n","
\n","
4
\n","
1009
\n","
Blount County Total Migration-US and Foreign
\n","
1252.0
\n","
2808
\n","
17390.0
\n","
42898.0
\n","
7.199540
\n","
2012
\n","
\n","
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
\n","
\n","
28193
\n","
56037
\n","
Sweetwater County Total Migration-US and Foreign
\n","
1084.0
\n","
2306
\n","
15424.0
\n","
35193.0
\n","
7.028008
\n","
2020
\n","
\n","
\n","
28194
\n","
56039
\n","
Teton County Total Migration-US and Foreign
\n","
1152.0
\n","
1867
\n","
10840.0
\n","
20640.0
\n","
10.627306
\n","
2020
\n","
\n","
\n","
28195
\n","
56041
\n","
Uinta County Total Migration-US and Foreign
\n","
476.0
\n","
1037
\n","
7102.0
\n","
17922.0
\n","
6.702337
\n","
2020
\n","
\n","
\n","
28196
\n","
56043
\n","
Washakie County Total Migration-US and Foreign
\n","
171.0
\n","
306
\n","
2885.0
\n","
6592.0
\n","
5.927210
\n","
2020
\n","
\n","
\n","
28197
\n","
56045
\n","
Weston County Total Migration-US and Foreign
\n","
186.0
\n","
343
\n","
2452.0
\n","
5267.0
\n","
7.585644
\n","
2020
\n","
\n"," \n","
\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","
origin_countyfips
\n","
county
\n","
outmigrant_returns
\n","
nonmigrant_returns
\n","
%outflow
\n","
Year
\n","
\n"," \n"," \n","
\n","
38205
\n","
15005
\n","
Kalawao County Tot Mig-US & For
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
39289
\n","
30069
\n","
Petroleum Count Tot Mig-US & Fo
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
39306
\n","
30103
\n","
Treasure County Tot Mig-US & Fo
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
39313
\n","
31005
\n","
Arthur County Tot Mig-US & For
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
39315
\n","
31009
\n","
Blaine County Tot Mig-US & For
\n","
1.0
\n","
1.0
\n","
0.0
\n","
2005
\n","
\n","
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
...
\n","
\n","
\n","
2391
\n","
46063
\n","
Harding County Total Migration-US and Foreign
\n","
1.0
\n","
503.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
2394
\n","
46069
\n","
Hyde County Total Migration-US and Foreign
\n","
1.0
\n","
518.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
2652
\n","
48261
\n","
Kenedy County Total Migration-US and Foreign
\n","
1.0
\n","
111.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
2656
\n","
48269
\n","
King County Total Migration-US and Foreign
\n","
1.0
\n","
89.0
\n","
0.0
\n","
2018
\n","
\n","
\n","
2672
\n","
48301
\n","
Loving County Total Migration-US and Foreign
\n","
1.0
\n","
28.0
\n","
0.0
\n","
2018
\n","
\n"," \n","
\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":[]}]}