# ---
# 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
# ---

pip install plotly==4.5.4

pip install "notebook>=5.3" "ipywidgets>=7.2"




# +
import plotly.graph_objects as go
import pandas as pd
import plotly.express as px
import glob
import json

df = pd.read_csv('graph_data.csv')
df.head()

# -

df.columns

# +
# fig = go.Figure(go.Scatter(x = df['issue_date'], y = df['AAPL_y'],
#                   name='Share Prices (in USD)'))

# fig.update_layout(title='Apple Share Prices over time (2014)',
#                    plot_bgcolor='rgb(230, 230,230)',
#                    showlegend=True)

# fig.show()
# -

df['grade'].value_counts()

df['loan_amnt'].sum()

df['loan_status'].value_counts()

# +
status_sum = df.groupby(['loan_status']).sum()['loan_amnt']
status_count = df.groupby(['loan_status']).count()['loan_amnt']
status_ave = df.groupby(['loan_status']).mean()['loan_amnt']

status_df = pd.DataFrame({"Total Loan value": status_sum,
                        "Number of Loans": status_count,
                          "Ave Loan value": status_ave
                               })

status_df = status_df.sort_values("Total Loan value")

status_df["Total Loan value"] = status_df["Total Loan value"].map("${:,.2f}".format)
status_df["Ave Loan value"] = status_df["Ave Loan value"].map("${:,.2f}".format)

status_df

# +
grade_sum = df.groupby(['grade']).sum()['loan_amnt']
grade_count = df.groupby(['grade']).count()['loan_amnt']
grade_ave = df.groupby(['grade']).mean()['loan_amnt']

grade_df = pd.DataFrame({"Total Loan value by Grade": grade_sum,
                        "Loans by Grade": grade_count, 
                         "Ave Loan Value": grade_ave
                               })

grade_df["Total Loan value by Grade"] = grade_df["Total Loan value by Grade"].map("${:,.2f}".format)
grade_df["Ave Loan Value"] = grade_df["Ave Loan Value"].map("${:,.2f}".format)

grade_df
# -

#save a sample to test out
df1 = df.sample(50000)
df1

#save a sample of data to play with
df1.to_csv('graph_data_sample.csv',index=False)

grade_df = df.groupby(['issue_date', 'grade'])['funded_amnt'].sum().to_frame()
grade_df.to_json('grade_table.json',orient = 'table')

status_df = df.groupby(['issue_date', 'loan_status'])['funded_amnt'].sum().to_frame()
status_df.to_json('status.json',orient = 'index')

df.purpose.unique()

# +
df.purpose.replace({'credit_card': "Credit Card", 'debt_consolidation': 'Debt Consolidation',
                    'home_improvement': 'Home/Moving',
                    'vacation': 'Vacation', 'medical': 'Medical', 'major_purchase': 'Major Purchase',
                    'other': 'Other', 'car': 'Major Purchase', 'moving': 'Home/Moving','house': 'Home/Moving', 
                    'small_business': 'Small Business', 'renewable_energy': 'Other',
                    'educational': 'Other','wedding': 'Other'}, inplace=True)



# -

df['purpose'].value_counts()

purpose_df = df.groupby(['issue_date', 'purpose'])['funded_amnt'].sum().to_frame()
purpose_df

status_df = df.groupby(['issue_date', 'loan_status'])['funded_amnt'].sum().to_frame()
status_json = status_df.reset_index().to_dict('records')
status_json

# +
import json

json = json.dumps(status_json)
f = open ('status_dict.json','w')
f.write(json)
f.close()
# -

grade_df = df.groupby(['issue_date', 'grade'])['funded_amnt'].sum().to_frame()
grade_json = grade_df.reset_index().to_dict('records')
grade_json

json1 = json.dumps(grade_json)
f = open ('grade_dict.json','w')
f.write(json1)
f.close()

# df.purpose.replace({'credit_card': "Credit Card", 'debt_consolidation': 'Debt Consolidation',
#                     'home_improvement': 'Home Improvement',
#                     'vacation': 'Vacation', 'medical': 'Medical', 'major_purchase': 'Major Purchase',
#                     'other': 'Other', 'car': 'Car', 'moving': 'Moving','house': 'House', 
#                     'small_business': 'Small Business', 'renewable_energy': 'Renewable Energy',
#                     'educational': 'Educational','wedding': 'Wedding'}, inplace=True)
purpose_df = df.groupby(['issue_date', 'purpose'])['funded_amnt'].sum().to_frame()
purpose_json = purpose_df.reset_index().to_dict('records')
purpose_json

# +
import json

json2 = json.dumps(purpose_json)
f = open ('purpose_dict.json', 'w')
f.write(json2)
f.close()
# -



StarcoderdataJupyterScriptsDedupFiltered