so i'm learning python and decided to program a payroll for my company as practice.
for the usa bi-weekly tax, there is pattern to the amount you need to pay, here is a short code for example
if n >= 760 and n < 780:
gt = 45
if n >= 780 and n < 800:
gt = 47
if n >= 800 and n < 820:
gt = 49
if n >= 820 and n < 840:
gt = 51
here n is the salary, and gt is the tax to be paid.
as you can see, the range of n is a constant 20, and tax increase by a constant 2
this is true from 720 - 1000, starting from 1000 however, the rate of the tax increases by constant 3
i want to to be able to include a salary range from 720 to 2000, is there a way to do this, or do i have to do it the hard way and write them all out?
I'd branch on your two ranges and then use some math to get the tax amount:
if n >= 720 and n < 1000:
gt = 2*(( n - 720 ) / 20) + 41
elif n >= 1000 and n < 2000:
gt = 3*(( n - 1000 ) / 20) + 67
Related
I have a dataframe with a specific value called rate_multiplier that I need to grab and compare it to the rate_multiplier I get from AWS s3 bucket.
In order to grab the rate_multiplier in the dataframe, I need to take the random variables that I created for a "person" and match them to the dataframe which gives a specific rate_mulitplier based on these certain characteristics
For example:
Random variables created:
Life_term = 25
Gender = F
Rate_class = Best-AB
Age = 27
Coverage = 2310
Dataframe:
Life_term Benefit_Length_months Gender Rate_class Age State Coverage band (low end) Coverage band (high end) Coverage Band Rate_multiplier
0 15 180 M Best-AA 18 Default 500 1199 500-1199 2.31
1 15 180 M Best-AA 19 Default 500 1199 500-1199 2.21
2 15 180 M Best-AA 20 Default 500 1199 500-1199 2.11
3 15 180 M Best-AA 21 Default 500 1199 500-1199 2.03
4 15 180 M Best-AA 22 Default 500 1199 500-1199 1.95
... ... ... ... ... ... ... ... ... ... ...
34987 10 120 F Nicotine Average-CD 61 Default 3600 10000 3600+ 19.10
34988 10 120 F Nicotine Average-CD 62 Default 3600 10000 3600+ 21.27
34989 10 120 F Nicotine Average-CD 63 Default 3600 10000 3600+ 23.44
34990 10 120 F Nicotine Average-CD 64 Default 3600 10000 3600+ 25.61
34991 10 120 F Nicotine Average-CD 65 Default 3600 10000 3600+ 27.78
So for this example, my randomly generated person would get a rate_multiplier of:
0.93
My code is as follows:
rate_mult_df.loc[(rate_mult_df['Life_term'] == 15) & (rate_mult_df['Gender'] == 'F') & (rate_mult_df['Rate_class'] == 'Best-AB') & (rate_mult_df['Age'] == 27) & (rate_mult_df['Coverage band (low end)'] <= 2310) & (rate_mult_df['Coverage band (high end)'] >= 2310)]
Is the right way to grab the rate_muliplier for the randomly genreated person or is there any easier way? Any and all help is appreciated. Please let me know if my question is clear enough. Working on that everyday.
For perfomance reasons I'd use .query()
rate_multiplier = df.query(
"Life_term == 15 &"
" Gender == 'F' &"
" Rate_class == 'Best-AB' &"
" Age == 27 &"
" `Coverage band (low end)` == 2310 &"
" `Coverage band (high end)` == 2310"
)["Rate_multiplier"].squeeze()
"Easier" depends on your workflow. For example if you want to query from a dictionary you could use:
def get_rate_multiplier(search_params: dict) -> str:
return " and ".join(
[f"({k} == '{v}')" if type(v) == str else f"({k} == {v})" for k, v in search_params.items()]
)
random_person = {
"Life_term": 15, "Gender": "F", "Rate_class": "Best-AB",
"Age": 27, "Coverage band (low end)": 2310, "Coverage band (high end)": 2310
}
rate_multiplier = float(df.query(get_rate_multiplier(random_person))["Rate_multiplier"].squeeze())
I would like to calculate the average slope of multiple numbers. For example:
I've been given 5 numbers (eg. 1.1523, 1.4626, 1.5734, 1.8583, 1.6899). I get a new number every 15 minutes and delete the oldest and want to calculate again the average slope.
I've already seen formulas but I don't really get how to calculate it when like the imaginary x-axis is the time. Like I have:
X: 14:00, 14:15, 14:30, 14:45, 15:00
Y: 1.1523, 1.4626, 1.5734, 1.8583, 1.6899
Assuming all times are in HH:MM format and we don't need to worry about passing midnight, this should work:
X = ['14:00', '14:15', '14:30', '14:45', '15:00']
Y = [1.1523, 1.4626, 1.5734, 1.8583, 1.6899]
minutes = [int(s[:2]) * 60 + int(s[3:]) for s in X]
slope = (Y[-1] - Y[0]) / ((minutes[-1] - minutes[0]) / 60)
print(slope)
slopes = [(Y[i] - Y[i - 1]) / ((minutes[i] - minutes[i - 1]) / 60) for i in range(1, len(X))]
print(slopes)
averageSlope = sum(slopes) / (len(X) - 1)
print(averageSlope)
Results:
0.5375999999999999
[1.2411999999999992, 0.44320000000000004, 1.1396000000000006, -0.6736000000000004]
0.5375999999999999
I could be wrong, but isn't average slope determined the same way as average velocity - which is Delta d / Delta t? If that is the case, shouldn't it be Delta Y / Delta X?
from datetime import datetime
X = ['14:00', '14:15', '14:30', '14:45', '15:00']
Y = [1.1523, 1.4626, 1.5734, 1.8583, 1.6899]
today = datetime.now()
avg = []
for i in range(len(X) - 1):
e = X[i + 1].split(":")
e = datetime(today.year, today.month, today.day, int(e[0]), int(e[1]))
s = X[i].split(":")
s = datetime(today.year, today.month, today.day, int(s[0]), int(s[1]))
deltaX = e - s
deltaY = Y[i + 1] - Y[i]
ans = (deltaY / deltaX.seconds) / 60
avg.append(f'{ans:.9f}')
print(avg)
['0.000005746', '0.000002052', '0.000005276', '-0.000003119']
Consider this data:
1 minute(s) 1 meter(s)
2 minute(s) 2 meter(s)
3 minute(s) 3 meter(s)
You should have a slope of 1 meter/minute, no matter how you cut the cake.
Likewise for this:
1 minute(s) 2 meter(s)
2 minute(s) 3 meter(s)
3 minute(s) 5 meter(s)
From 2 to 1 minutes, the average is 1 meters/minute, from 3 to 2 minutes its 2 meters/minute, and from 3 to 1 minutes its 1.5 meters/minute.
The installment amount is calculated by the formula below.
I have a dataframe where I have the principal amount (P), installment amount and number of payments (n) in different columns and I wish to calculate the interest rate (i) for all rows.
Principal (P)
Installment Amount
Number of Installments (n)
Interest Rate (i)
5.300
187
35
r
Given a dataframe called df
>>> df
Principal Installment Num Payments
0 1000.0 40.0 30
1 3500.0 200.0 20
2 10000000.0 2000000.0 10
and a function interest using some solving method (in below example, Newton-Raphson)
ERROR_TOLERANCE = 1e-6
def interest(principal, installment, num_payments):
def f(x):
return principal * x**(num_payments + 1) - (principal + installment) * x**num_payments + installment
def f_prime(x):
return principal * (num_payments + 1) * x**num_payments - (principal + installment)*num_payments * x**(num_payments - 1)
guess = 1 + (((installment * num_payments / principal) - 1)/12)
intermediate = f(guess)
while abs(intermediate) > ERROR_TOLERANCE:
guess = guess - intermediate / f_prime(gues
intermediate = f(guess)
return guess
you can calculate the interest rate like
df['Interest'] = df.apply(lambda row: interest(row['Principal'],row['Installment'],row['Num Payments']),axis=1)
giving
>>> df
Principal Installment Num Payments Interest
0 1000.0 40.0 30 1.012191
1 3500.0 200.0 20 1.013069
2 10000000.0 2000000.0 10 1.150984
Note: tweak ERROR_TOLERANCE as desired to meet requirements.
I'm very new to Python, Just as a way of learning i tasked myself with this problem but no matter what i do the result still comes up to 100000 even when the value is less than the (first condition or second condition) and should print 200000. Please, help.
price = 1000000
credit_score = 300
income = 70000
if credit_score and income:
credit_score > 700 and income > 80000
downpayment = price * 0.10
print(f"Downpayment: {downpayment}")
elif credit_score or income:
credit_score < 700 or income < 80000
downpayment = price * 0.20
print(f"Downpayment: {downpayment}")
else:
downpayment = price * 0.30
print(f"Downpayment: {downpayment}")
You're putting the conditions that you want to test after the if statements, not in them where they belong.
if credit_score > 700 and income > 80000:
downpayment = price * 0.10
print(f"Downpayment: {downpayment}")
elif credit_score < 700 or income < 80000:
downpayment = price * 0.20
print(f"Downpayment: {downpayment}")
else:
downpayment = price * 0.30
print(f"Downpayment: {downpayment}")
Instead of
if credit_score and income:
credit_score > 700 and income > 80000
Do
if credit_score > 700 and income > 80000:
Putting a variable directly as the clause in an if statement (i.e. if credit_score) tries to coerce that variable into a boolean. Any nonzero number or any non-empty string registers as true, which means your code is always taking the first branch.
Instead, what you should be doing is checking the condition credit_score > 700 and the condition income > 80000.
I hope you're already clear about how it works now. I'll just provide another way of doing this:
downpayment = price * 0.10 if (credit_score > 700 and income > 80000) else (price * 0.20 if credit_score < 700 or income < 80000 else price * 0.30)
print(f"Downpayment: {downpayment}")
I'd like to see if there's a way to calculate something like the following on Python, is it possible?
ID Rotation Starting_degree Current_degree
1 40 360 320
1 55 320 265
2 70 360 290
1 15 265 250
2 20 290 270
3 30 360 330
3 60 330 270
1 25 250 225
In general my code is df['current_degree'] = df.apply(lambda row: row.starting_degree - row.rotation, axis = 1), but I'd like the starting degree figure to change based on ID and any previous calculations.
With each new ID the starting degree resets to 360.
IIUC, you want to calculate the current degree given the rotation:
# assume that all IDs start with 360
df['Start'] = 360
# grouping by ID
groups = df.groupby("ID")
# compute the total rotation by cumsum
df['Rot_so_far'] = groups.Rotation.cumsum()
# current degree
df['Current_degree'] = df['Start'] - df['Rot_so_far']
You may want to do a modulo by 360 for non-negative current degree.