-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonalizedFinance
178 lines (141 loc) · 5.62 KB
/
PersonalizedFinance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// pip install requests pandas
import requests
import json
import pandas as pd
# Replace with your Typeform API token and form ID
API_TOKEN = "your_typeform_api_token"
FORM_ID = "your_typeform_form_id"
# Function to fetch responses from Typeform
def get_typeform_responses():
headers = {
"Authorization": f"Bearer {API_TOKEN}"
}
url = f"https://api.typeform.com/forms/{FORM_ID}/responses"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to fetch data. Status code: {response.status_code}")
# Fetch responses
typeform_data = get_typeform_responses()
def parse_responses(typeform_data):
parsed_data = []
for response in typeform_data['items']:
answers = {}
for answer in response['answers']:
# Example: Mapping question IDs to meaningful field names
if answer['field']['id'] == "income_question_id":
answers['income'] = float(answer['number']) # Assuming the answer is a number
elif answer['field']['id'] == "expenses_question_id":
answers['expenses'] = float(answer['number'])
elif answer['field']['id'] == "savings_goal_question_id":
answers['savings_goal'] = float(answer['number'])
parsed_data.append(answers)
return pd.DataFrame(parsed_data)
# Parse responses into a DataFrame
df = parse_responses(typeform_data)
print(df.head())
def analyze_finances(df):
# Calculate disposable income
df['disposable_income'] = df['income'] - df['expenses']
# Calculate the percentage of income saved towards the goal
df['savings_percentage'] = (df['savings_goal'] / df['income']) * 100
# Generate simple advice
df['advice'] = df.apply(lambda row: generate_advice(row), axis=1)
return df
def generate_advice(row):
if row['disposable_income'] < 0:
return "You are spending more than you earn. Consider cutting back on expenses."
elif row['savings_percentage'] < 20:
return "Try to save at least 20% of your income to secure your financial future."
else:
return "You're on track with your savings goal! Keep it up."
# Analyze the finances and generate advice
df_analysis = analyze_finances(df)
print(df_analysis)
def generate_report(df):
reports = []
for _, row in df.iterrows():
report = f"""
Personalized Finance Report:
---------------------------
Income: ${row['income']}
Expenses: ${row['expenses']}
Disposable Income: ${row['disposable_income']}
Savings Goal: ${row['savings_goal']}
Savings Percentage: {row['savings_percentage']:.2f}%
Financial Advice: {row['advice']}
"""
reports.append(report)
return reports
# Generate the report for all users
reports = generate_report(df_analysis)
for report in reports:
print(report)
import requests
import pandas as pd
# Replace with your Typeform API token and form ID
API_TOKEN = "your_typeform_api_token"
FORM_ID = "your_typeform_form_id"
# Function to fetch responses from Typeform
def get_typeform_responses():
headers = {
"Authorization": f"Bearer {API_TOKEN}"
}
url = f"https://api.typeform.com/forms/{FORM_ID}/responses"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to fetch data. Status code: {response.status_code}")
# Parse the Typeform data into a DataFrame
def parse_responses(typeform_data):
parsed_data = []
for response in typeform_data['items']:
answers = {}
for answer in response['answers']:
if answer['field']['id'] == "income_question_id":
answers['income'] = float(answer['number'])
elif answer['field']['id'] == "expenses_question_id":
answers['expenses'] = float(answer['number'])
elif answer['field']['id'] == "savings_goal_question_id":
answers['savings_goal'] = float(answer['number'])
parsed_data.append(answers)
return pd.DataFrame(parsed_data)
# Analyze financial data
def analyze_finances(df):
df['disposable_income'] = df['income'] - df['expenses']
df['savings_percentage'] = (df['savings_goal'] / df['income']) * 100
df['advice'] = df.apply(lambda row: generate_advice(row), axis=1)
return df
def generate_advice(row):
if row['disposable_income'] < 0:
return "You are spending more than you earn. Consider cutting back on expenses."
elif row['savings_percentage'] < 20:
return "Try to save at least 20% of your income to secure your financial future."
else:
return "You're on track with your savings goal! Keep it up."
# Generate personalized finance reports
def generate_report(df):
reports = []
for _, row in df.iterrows():
report = f"""
Personalized Finance Report:
---------------------------
Income: ${row['income']}
Expenses: ${row['expenses']}
Disposable Income: ${row['disposable_income']}
Savings Goal: ${row['savings_goal']}
Savings Percentage: {row['savings_percentage']:.2f}%
Financial Advice: {row['advice']}
"""
reports.append(report)
return reports
# Fetch responses, parse data, analyze finances, and generate reports
typeform_data = get_typeform_responses()
df = parse_responses(typeform_data)
df_analysis = analyze_finances(df)
reports = generate_report(df_analysis)
# Print all reports
for report in reports:
print(report)