# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd 
import pymongo
from pymongo import MongoClient
import os

# Create instance of MongoClient
client = MongoClient()
# Connection URI
connStr = os.getenv('MONGO_CONN')
client = MongoClient(connStr)
# Select database
db = client['MSA']
# Select the collection within the database (in this case its GDP_raw data, check list below for other collections)
gdp = db.GDP_raw
# Convert entire collection to Pandas dataframe
df_gdp = pd.DataFrame(list(gdp.find()))
df_gdp.head()

df_gdp.sort_values(inplace=True, by='GeoFips')
df_gdp.head()

df_gdp.columns

df_gdp.drop(columns=['_id', 'GeoName', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009'], inplace=True)
df_gdp = df_gdp[df_gdp['GeoFips'] != 998]
df_gdp.head()

df_gdp.rename(columns={'GeoFips' : 'CBSA'}, inplace=True)
df_gdp.head()

df_gdp.info()

# create new collection
gdp_collection = db.GDP_clean
# turn dataframe into readable format for mongo
df_dict = df_gdp.to_dict(orient='records')
# write dataframe to unemployment_predicted_2024 collection
gdp_collection.insert_many(df_dict)



StarcoderdataJupyterScriptsDedupFiltered