I am to make a python program using basic operators to ask for an income from the user, in order to calculate the 2017 progressive tax along with 2018. The output should look like this:
Income: 15000
2017 tax: (tax amount here) #will skip the math for samples
2018 tax: (tax amount here)
My program as of now produces both prints for 2017 / 2018 but will stop at the 2018 nested if bracket of 82501 to 157500 (which is nested in the 4th elif)... Here's my program as of now, since its long I'll mark out where it stops working.
income = float(input("Enter your income to calculate tax: "))
#Loop input/calculations
while income > 0:
print("----------------------------------------------")
if income >= 0 and income <= 9325:
bracket1 = income * 0.10
tax2017 = bracket1
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 0 and income <= 9525:
newbracket1 = income * 0.10
tax2018 = newbracket1
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
elif income >= 9326 and income <= 37950:
bracket1 = 9325 * 0.10
bracket2 = (income - 9326) * 0.15
tax2017 = bracket1 + bracket2
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 9526 and income <=38700:
newbracket1 = 9526 * 0.10
newbracket2 = (income - 9525) * 0.12
tax2018 = newbracket1 + newbracket2
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
elif income >= 37951 and income <= 91900:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (income - 37951) * 0.25
tax2017 = bracket1 + bracket2 + bracket3
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 38701 and income <= 82500:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (income - 38700) * 0.22
tax2018 = newbracket1 + newbracket2 + newbracket3
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
elif income >= 91901 and income <= 191650:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (income - 91901) * 0.28
tax2017 = bracket1 + bracket2 + bracket3 + bracket4
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 82501 and income <= 157500: #HERE STOPS WORKING, the 2018 from here on doesn't print
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (82500 - 38701) * 0.22
newbracket4 = (income - 82500) * 0.24
tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
elif income >= 191651 and income <= 416700:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (191650 - 91901) * 0.28
bracket5 = (income - 191651) * 0.33
tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 157501 and income <= 200000:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (82500 - 38701) * 0.22
newbracket4 = (157500 - 82501) * 0.24
newbracket5 = (income - 157500) * 0.32
tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
elif income >= 416701 and income <= 418400:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (191650 - 91901) * 0.28
bracket5 = (416700 - 191650) * 0.33
bracket6 = (income - 416701) * 0.35
tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 200001 and income <= 500000:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (82500 - 38701) * 0.22
newbracket4 = (157500 - 82501) * 0.24
newbracket5 = (200000 - 157501) * 0.32
newbracket6 = (income - 200001) * 0.35
tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5 + newbracket6
print("2018 tax: ",tax2018)
income = float(input("\nEnter your income as and integer with no commas: "))
elif income >= 418401:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (191650 - 91901) * 0.28
bracket5 = (416700 - 191650) * 0.33
bracket6 = (418400 - 416700) * 0.35
bracket7 = (income - 418401) * 0.396
tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6 + bracket7
print("Income: ",income)
print("2017 tax: ",tax2017)
income = float(input("\nEnter your income as and integer with no commas: "))
else:
print("invalid")
I've marked the line that wont work. just to clarify, the nested if's and prints before that output both 2017 and 2018 results, but when the income is in the marked range and greater, only 2017's tax will print.
My outcome at income 82502 and above is something like:
Enter your income to calculate tax: 82502
----------------------------------------------
Income: 82502.0
2017 tax: 16364.00
Enter your income as and integer with no commas:
SOLVED:
thanks for all the comments and answers, they cleared some things up for me!
here is what I've reworked, seems to work so I'm satisfied. I'll be adding a few more calculations so hopefully that goes well too :)
income = float(input("Enter your income to calculate tax: "))
#Loop input/calculations
while income > 0:
if income >= 0 and income <= 9325:
bracket1 = income * 0.10
tax2017 = bracket1
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 0 and income <= 9525:
newbracket1 = income * 0.10
tax2018 = newbracket1
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
if income >= 9326 and income <= 37950:
bracket1 = 9325 * 0.10
bracket2 = (income - 9326) * 0.15
tax2017 = bracket1 + bracket2
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 9526 and income <=38700:
newbracket1 = 9526 * 0.10
newbracket2 = (income - 9525) * 0.12
tax2018 = newbracket1 + newbracket2
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
if income >= 37951 and income <= 91900:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (income - 37951) * 0.25
tax2017 = bracket1 + bracket2 + bracket3
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 38701 and income <= 82500:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (income - 38700) * 0.22
tax2018 = newbracket1 + newbracket2 + newbracket3
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
if income >= 91901 and income <= 191650:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (income - 91901) * 0.28
tax2017 = bracket1 + bracket2 + bracket3 + bracket4
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 82501 and income <= 157500:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (82500 - 38701) * 0.22
newbracket4 = (income - 82500) * 0.24
tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
if income >= 191651 and income <= 416700:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (191650 - 91901) * 0.28
bracket5 = (income - 191651) * 0.33
tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 157501 and income <= 200000:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (82500 - 38701) * 0.22
newbracket4 = (157500 - 82501) * 0.24
newbracket5 = (income - 157500) * 0.32
tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
if income >= 416701 and income <= 418400:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (191650 - 91901) * 0.28
bracket5 = (416700 - 191650) * 0.33
bracket6 = (income - 416701) * 0.35
tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 200001 and income < 500000:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (82500 - 38701) * 0.22
newbracket4 = (157500 - 82501) * 0.24
newbracket5 = (200000 - 157501) * 0.32
newbracket6 = (income - 200001) * 0.35
tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5 + newbracket6
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
if income >= 418401:
bracket1 = 9325 * 0.10
bracket2 = (37950 - 9325) * 0.15
bracket3 = (91901 - 37950) * 0.25
bracket4 = (191650 - 91901) * 0.28
bracket5 = (416700 - 191650) * 0.33
bracket6 = (418400 - 416700) * 0.35
bracket7 = (income - 418401) * 0.396
tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6 + bracket7
print("Income: ",income)
print("2017 tax: ",format(tax2017,'.2f'))
if income >= 500000:
newbracket1 = 9526 * 0.10
newbracket2 = (38700 - 9526) * 0.12
newbracket3 = (82500 - 38701) * 0.22
newbracket4 = (157500 - 82501) * 0.24
newbracket5 = (200000 - 157501) * 0.32
newbracket6 = (500000 - 200001) * 0.35
newbracket7 = (income - 500000) * 0.37
tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5 + newbracket6 + newbracket7
print("2018 tax: ",format(tax2018,'.2f'))
income = float(input("\nEnter your income as and integer with no commas: "))
else:
print("-------------------")
Just work through what statements the program will go through for
income = 82502.0
will enter
elif income >= 37951 and income <= 91900:
but for 2018 calculation, it won't satisfy the conditional inside it
if income >= 38701 and income <= 82500:
So it has no way to calculate 2018. It will not get to the point where you marked it.
2017 calculation should be independent of 2018 calculations. There are lots of cases where your calculation will not print 2018 tax.
But you did have good testing to see that your program is not working. Edge cases generally give problems.
Related
So I built a simple income tax calculator as a project for my class which includes all 50 states and DC. My code seems extra, I have only included the code for Florida. How can I make this efficient? Additionally, are there any areas where I can include for or while statement?
state = input("Enter state: ")
if state == "Florida" or "florida" or "FL" or "fl":
def fl(fl_income):
return 0
def print_info(fl_income, afterTaxes):
fedTaxes_paid = afterTaxes
print("Total federal taxes you pays: $", round(fedTaxes_paid, 2))
fl_loss = fl(fl_income)
ssc_tax = income * 0.062
medi_tax = income * 0.0145
fica = ssc_tax + medi_tax
final_income = income - fedTaxes_paid - fl_loss - fica
print("Total Florida state taxes you pay: $", round(fl_loss, 2))
print("Total FICA tax deducted: $", round(fica, 2))
print("Income after fed taxes: $", round(income - fedTaxes_paid, 2))
print("Income after Florida state taxes: $", round(final_income, 2))
status = input("Enter your filing status: ")
income = float(input("Enter your income: $"))
if status == "single":
if str(int(income)) == "0":
sys.exit()
elif income <= 9875:
afterTaxes = income * 0.10
print_info(income,afterTaxes)
elif income <= 40125:
afterTaxes = 9875 * 0.10 + (income - 9875) * 0.12
print_info(income,afterTaxes)
elif income <= 85525:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (income - 40125) * 0.22
print_info(income,afterTaxes)
elif income <= 163300:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (85525 - 40125) * 0.22 + \
(income - 85525) * 0.24
print_info(income,afterTaxes)
elif income <= 207350:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (85525 - 40125) * 0.22 + \
(163300 - 85525) * 0.24 + (income - 163300) * 0.32
print_info(income,afterTaxes)
elif income <= 518400:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (85525 - 40125) * 0.22 + \
(163300 - 85525) * 0.24 + (207350 - 163300) * 0.32 + (income - 207350) * 0.35
print_info(income,afterTaxes)
else:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (85525 - 40125) * 0.22 + \
(163300 - 85525) * 0.24 + (207350 - 163300) * 0.32 + (518400 - 207350) * 0.35 + \
(income - 518400) * 0.37
print_info(income,afterTaxes)
print("")
elif status == "married filed jointly" or "filed jointly":
if str(int(income)) == "0":
sys.exit()
elif income <= 19750:
afterTaxes = income * 0.10
print_info(income,afterTaxes)
elif income <= 80250:
afterTaxes = 19750 * 0.10 + (income - 19750) * 0.12
print_info(income,afterTaxes)
elif income <= 171050:
afterTaxes = 19750 * 0.10 + (80250 - 19750) * 0.12 + (income - 80250) * 0.22
print_info(income,afterTaxes)
elif income <= 326600:
afterTaxes = 19750 * 0.10 + (80250 - 19750) * 0.12 + (171050 - 80250) * 0.22 + \
(income - 171050) * 0.24
print_info(income,afterTaxes)
elif income <= 414700:
afterTaxes = 19750 * 0.10 + (80250 - 19750) * 0.12 + (171050 - 80250) * 0.22 + \
(326600 - 171050) * 0.24 + (income - 326600) * 0.32
print_info(income,afterTaxes)
elif income <= 622050:
afterTaxes = 19750 * 0.10 + (80250 - 19750) * 0.12 + (171050 - 80250) * 0.22 + \
(326600 - 171050) * 0.24 + (414700 - 326600) * 0.32 + (income - 414700) * 0.35
print_info(income,afterTaxes)
else:
afterTaxes = 19750 * 0.10 + (80250 - 19750) * 0.12 + (171050 - 80250) * 0.22 + \
(326600 - 171050) * 0.24 + (414700 - 326600) * 0.32 + (518400 - 414700) * 0.35 + \
(income - 622050) * 0.37
print_info(income,afterTaxes)
print("")
elif status == "married filed separately" or "filed separately":
if str(int(income)) == "0":
sys.exit()
elif income <= 9875:
afterTaxes = income * 0.10
print_info(income,afterTaxes)
elif income <= 40125:
afterTaxes = 9875 * 0.10 + (income - 9875) * 0.12
print_info(income,afterTaxes)
elif income <= 85525:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (income - 40125) * 0.22
print_info(income,afterTaxes)
elif income <= 163300:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (85525 - 40125) * 0.22 + \
(income - 171050) * 0.24
print_info(income,afterTaxes)
elif income <= 207350:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (85525 - 40125) * 0.22 + \
(163300 - 85525) * 0.24 + (income - 163300) * 0.32
print_info(income,afterTaxes)
elif income <= 311025:
afterTaxes = 9875 * 0.10 + (40125 - 9875) * 0.12 + (85525 - 40125) * 0.22 + \
(163300 - 85525) * 0.24 + (207350 - 163300) * 0.32 + (income - 207350) * 0.35
print_info(income,afterTaxes)
else:
afterTaxes = 9875 * 0.10 + (40125 - 19750) * 0.12 + (85525 - 40125) * 0.22 + \
(163300 - 85525) * 0.24 + (207350 - 163300) * 0.32 + (311025 - 207350) * 0.35 + \
(income - 311025) * 0.37
print_info(income,afterTaxes)
print("")
elif status == "head of household":
if str(int(income)) == "0":
sys.exit()
elif income <= 14100:
afterTaxes = income * 0.10
print_info(income,afterTaxes)
elif income <= 53700:
afterTaxes = 14100 * 0.10 + (income - 14100) * 0.12
print_info(income,afterTaxes)
elif income <= 85500:
afterTaxes = 14100 * 0.10 + (53700 - 14100) * 0.12 + (income - 53700) * 0.22
print_info(income,afterTaxes)
elif income <= 163300:
afterTaxes = 14100 * 0.10 + (53700 - 14100) * 0.12 + (85500 - 53700) * 0.22 + \
(income - 85500) * 0.24
print_info(income,afterTaxes)
elif income <= 207350:
afterTaxes = 14100 * 0.10 + (53700 - 14100) * 0.12 + (85500 - 53700) * 0.22 + \
(163300 - 85500) * 0.24 + (income - 163300) * 0.32
print_info(income,afterTaxes)
elif income <= 518400:
afterTaxes = 14100 * 0.10 + (53700 - 14100) * 0.12 + (85500 - 53700) * 0.22 + \
(163300 - 85500) * 0.24 + (207350 - 163300) * 0.32 + (income - 207350) * 0.35
print_info(income,afterTaxes)
else:
afterTaxes = 14100 * 0.10 + (53700 - 14100) * 0.12 + (85500 - 53700) * 0.22 + \
(163300 - 85500) * 0.24 + (207350 - 163300) * 0.32 + (518400 - 207350) * 0.35 + \
(income - 518400) * 0.37
print_info(income,afterTaxes)
print("")
I inherited this code which is used to format and read a model output. However, when I run it and save the output to a text file it only gives the first 5 lines and last five lines for each location (dictionary key). I am not sure if it is a problem in the code or the settings. I am running the code in Jupyter notebook. Attached is the code and the output.
def outflw1(path=''):
#get the starting year (old outflw1 format)
if path == '':
fin1 = open('input')
else:
fin1 = open(os.path.join(path, 'input'))
s = fin1.readline()
while 'starting date of simulation' not in s:
s = fin1.readline()
s = s.replace(' ','').split(',')
year = int(s[2][:4])
fin1.close()
#read off 5 lines - don't need them. These are the headers and blank lines in the file
f = open(os.path.join(path,'outflw1'))
for i in range(5):
next(f)
#create dictionary for StringIO
sio = {} #denotes as a dictionary object
init = 0
for ln in f:
if not ln.strip():
init = 1
if s[0] == '12' and s[1] == '31' and s[2] == '23.0':
year += 1
continue
else:
s = ln.split()
#if first time through need to initialize StringIO objects
if init == 0:
sio[s[3]] = StringIO()
sio[s[3]].write(str(year) + '-' + s[0] + '-' + s[1] + ' ' + s[2].split('.')[0] + ':00:00' + ',')
else:
sio[s[3]].write(str(year) + '-' + s[0] + '-' + s[1] + ' ' + s[2].split('.')[0] + ':00:00' + ',')
sio[s[3]].write(','.join(s[4:8]) + ',')
if len(s) == 11:
sio[s[3]].write(','.join(s[9:]) + '\n')
else:
sio[s[3]].write(','.join([s[8][2:],s[9]]) + '\n')
continue
outflw1 = {}
for k in list(sio.keys()):
sio[k].seek(0)
outflw1[k] = pd.read_csv(sio[k], parse_dates=True, index_col=0,
names = ['tide', 'elevation', 'depth', 'velocity', 'direction', 'salinity'])
outflw1[k].index.name = 'Date'
return(outflw1)
Here is the output for one of the keys. It looks the same when I use the print command and save it to a text file.
{'10018': tide elevation depth velocity direction salinity
Date
1987-1-1 :00:00 0.40 0.06 3.06 0.01 121.87 8.48
1987-1-1 1:00:00 -0.47 0.09 3.09 0.02 122.65 8.50
1987-1-1 2:00:00 -1.54 0.13 3.13 0.03 134.67 8.51
1987-1-1 3:00:00 -1.83 0.18 3.18 0.02 133.44 8.53
1987-1-1 4:00:00 -1.75 0.21 3.21 0.01 334.74 8.55
... ... ... ... ... ... ...
2014-12-31 20:00:00 0.87 1.33 4.33 0.04 128.71 24.77
2014-12-31 21:00:00 0.86 1.40 4.40 0.05 169.77 24.84
2014-12-31 22:00:00 0.92 1.45 4.45 0.05 168.49 24.90
2014-12-31 23:00:00 1.02 1.37 4.37 0.03 311.43 24.91
2015-1-1 :00:00 0.97 1.34 4.34 0.02 161.63 24.93
[245449 rows x 6 columns],
Try to use pickle module to serialize:
pickle.dump
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 am trying to program nonlinear shooting method based on algorithm 11.2 from Numerical Analysis (Burden and Faires). However, after running the program, the numerical result i am getting is different from the answer in the textbook. i think there is something wrong in my coding but i cannot figure it out. I attached actual algorithm in the picture. Algorithm 11.2
Algorithm 11.2
Algorithm 11.2
Here is the code
from numpy import zeros, abs
def shoot_nonlinear(a,b,alpha, beta, n, tol, M):
w1 = zeros(n+1)
w2 = zeros(n+1)
h = (b-a)/n
k = 1
TK = (beta - alpha)/(b - a)
print("i"" x" " " "W1"" " "W2")
while k <= M:
w1[0] = alpha
w2[0] = TK
u1 = 0
u2 = 1
for i in range(1,n+1):
x = a + (i-1)*h #step 5
t = x + 0.5*(h)
k11 = h*w2[i-1] #step 6
k12 = h*f(x,w1[i-1],w2[i-1])
k21 = h*(w2[i-1] + (1/2)*k12)
k22 = h*f(t, w1[i-1] + (1/2)*k11, w2[i-1] + (1/2)*k12)
k31 = h*(w2[i-1] + (1/2)*k22)
k32 = h*f(t, w1[i-1] + (1/2)*k21, w2[i-1] + (1/2)*k22)
t = x + h
k41 = h*(w2[i-1]+k32)
k42 = h*f(t, w1[i-1] + k31, w2[i-1] + k32)
w1[i] = w1[i-1] + (k11 + 2*k21 + 2*k31 + k41)/6
w2[i] = w2[i-1] + (k12 + 2*k22 + 2*k32 + k42)/6
kp11 = h*u2
kp12 = h*(fy(x,w1[i-1],w2[i-1])*u1 + fyp(x,w1[i-1], w2[i-1])*u2)
t = x + 0.5*(h)
kp21 = h*(u2 + (1/2)*kp12)
kp22 = h*((fy(t, w1[i-1],w2[i-1])*(u1 + (1/2)*kp11)) + fyp(x+h/2, w1[i-1],w2[i-1])*(u2 +(1/2)*kp12))
kp31 = h*(u2 + (1/2)*kp22)
kp32 = h*((fy(t, w1[i-1],w2[i-1])*(u1 + (1/2)*kp21)) + fyp(x+h/2, w1[i-1],w2[i-1])*(u2 +(1/2)*kp22))
t = x + h
kp41 = h*(u2 + kp32)
kp42 = h*(fy(t, w1[i-1], w2[i-1])*(u1+kp31) + fyp(x + h, w1[i-1], w2[i-1])*(u2 + kp32))
u1 = u1 + (1/6)*(kp11 + 2*kp21 + 2*kp31 + kp41)
u2 = u2 + (1/6)*(kp12 + 2*kp22 + 2*kp32 + kp42)
r = abs(w1[n]) - beta
#print(r)
if r < tol:
for i in range(0,n+1):
x = a + i*h
print("%.2f %.2f %.4f %.4f" %(i,x,w1[i],w2[i]))
return
TK = TK -(w1[n]-beta)/u1
k = k+1
print("Maximum number of iterations exceeded")
return
function for 2nd order boundary value problem
def f(x,y,yp):
fx = (1/8)*(32 + 2*x**3 -y*yp)
return fx
def fy(xp,z,zp):
fyy = -(1/8)*(zp)
return fyy
def fyp(xpp,zpp,zppp):
fypp = -(1/8)*(zpp)
return fypp
a = 1 # start point
b = 3 # end point
alpha = 17 # boundary condition
beta = 43/3 # boundary condition
N = 20 # number of subintervals
M = 10 # maximum number of iterations
tol = 0.00001 # tolerance
shoot_nonlinear(a,b,alpha,beta,N,tol,M)
My result
i x W1 W2
0.00 1.00 17.0000 -16.2058
1.00 1.10 15.5557 -12.8379
2.00 1.20 14.4067 -10.2482
3.00 1.30 13.4882 -8.1979
4.00 1.40 12.7544 -6.5327
5.00 1.50 12.1723 -5.1496
6.00 1.60 11.7175 -3.9773
7.00 1.70 11.3715 -2.9656
8.00 1.80 11.1203 -2.0783
9.00 1.90 10.9526 -1.2886
10.00 2.00 10.8600 -0.5768
11.00 2.10 10.8352 0.0723
12.00 2.20 10.8727 0.6700
13.00 2.30 10.9678 1.2251
14.00 2.40 11.1165 1.7444
15.00 2.50 11.3157 2.2331
16.00 2.60 11.5623 2.6951
17.00 2.70 11.8539 3.1337
18.00 2.80 12.1883 3.5513
19.00 2.90 12.5635 3.9498
20.00 3.00 12.9777 4.3306
Actual result for w1
x W1
1.0 17.0000
1.1 15.7555
1.2 14.7734
1.3 13.3886
1.4 12.9167
1.5 12.5601
1.6 12.3018
1.7 12.1289
1.8 12.0311
1.9 12.0000
2.0 12.0291
2.1 12.1127
2.2 12.2465
2.3 12.4267
2.4 12.6500
2.5 12.9139
2.6 13.2159
2.7 13.5543
2.8 13.9272
2.9 14.3333
3.0 14.7713
On Line 48, you have
r = abs(w1[n]) - beta
Instead of
r = abs(w1[n] - beta)
Making this change gives the same solution as the text,
x W1
1.0 17.0000
1.1 15.7555
1.2 14.7734
1.3 13.9978
1.4 13.3886
1.5 12.9167
1.6 12.5601
1.7 12.3018
1.8 12.1289
1.9 12.0311
2.0 12.0000
2.1 12.0291
2.2 12.1127
2.3 12.2465
2.4 12.4267
2.5 12.6500
2.6 12.9139
2.7 13.2159
2.8 13.5543
2.9 13.9272
3.0 14.3333
As a remark in fx = (1/8)*(32 + 2*x**3 -y*yp), 1/8 is going to give the result 0.
You should use 1./8 instead.
import pandas as pd
df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0')
percent= 100*(len(df.loc[:,df.isnull().sum(axis=0)>=1 ].index) / len(df.index))
print(round(percent,2))
input is https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0
and the output should be
Ord_id 0.00
Prod_id 0.00
Ship_id 0.00
Cust_id 0.00
Sales 0.24
Discount 0.65
Order_Quantity 0.65
Profit 0.65
Shipping_Cost 0.65
Product_Base_Margin 1.30
dtype: float64
How about this? I think I actually found something similar on here once before, but I'm not seeing it now...
percent_missing = df.isnull().sum() * 100 / len(df)
missing_value_df = pd.DataFrame({'column_name': df.columns,
'percent_missing': percent_missing})
And if you want the missing percentages sorted, follow the above with:
missing_value_df.sort_values('percent_missing', inplace=True)
As mentioned in the comments, you may also be able to get by with just the first line in my code above, i.e.:
percent_missing = df.isnull().sum() * 100 / len(df)
Update let's use mean with isnull:
df.isnull().mean() * 100
Output:
Ord_id 0.000000
Prod_id 0.000000
Ship_id 0.000000
Cust_id 0.000000
Sales 0.238124
Discount 0.654840
Order_Quantity 0.654840
Profit 0.654840
Shipping_Cost 0.654840
Product_Base_Margin 1.297774
dtype: float64
IIUC:
df.isnull().sum() / df.shape[0] * 100.00
Output:
Ord_id 0.000000
Prod_id 0.000000
Ship_id 0.000000
Cust_id 0.000000
Sales 0.238124
Discount 0.654840
Order_Quantity 0.654840
Profit 0.654840
Shipping_Cost 0.654840
Product_Base_Margin 1.297774
dtype: float64
single line solution
df.isnull().mean().round(4).mul(100).sort_values(ascending=False)
To cover all missing values and round the results:
((df.isnull() | df.isna()).sum() * 100 / df.index.size).round(2)
The output:
Out[556]:
Ord_id 0.00
Prod_id 0.00
Ship_id 0.00
Cust_id 0.00
Sales 0.24
Discount 0.65
Order_Quantity 0.65
Profit 0.65
Shipping_Cost 0.65
Product_Base_Margin 1.30
dtype: float64
The solution you're looking for is :
round(df.isnull().mean()*100,2)
This will round up the percentage upto 2 decimal places
Another way to do this is
round((df.isnull().sum()*100)/len(df),2)
but this is not efficient as using mean() is.
import numpy as np
import pandas as pd
raw_data = {'first_name': ['Jason', np.nan, 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', np.nan, np.nan, 'Milner', 'Cooze'],
'age': [22, np.nan, 23, 24, 25],
'sex': ['m', np.nan, 'f', 'm', 'f'],
'Test1_Score': [4, np.nan, 0, 0, 0],
'Test2_Score': [25, np.nan, np.nan, 0, 0]}
results = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'sex', 'Test1_Score', 'Test2_Score'])
results
first_name last_name age sex Test1_Score Test2_Score
0 Jason Miller 22.0 m 4.0 25.0
1 NaN NaN NaN NaN NaN NaN
2 Tina NaN 23.0 f 0.0 NaN
3 Jake Milner 24.0 m 0.0 0.0
4 Amy Cooze 25.0 f 0.0 0.0
You can use following function, which will give you output in Dataframe
Zero Values
Missing Values
% of Total Values
Total Zero Missing Values
% Total Zero Missing Values
Data Type
Just copy and paste following function and call it by passing your pandas Dataframe
def missing_zero_values_table(df):
zero_val = (df == 0.00).astype(int).sum(axis=0)
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
mz_table = pd.concat([zero_val, mis_val, mis_val_percent], axis=1)
mz_table = mz_table.rename(
columns = {0 : 'Zero Values', 1 : 'Missing Values', 2 : '% of Total Values'})
mz_table['Total Zero Missing Values'] = mz_table['Zero Values'] + mz_table['Missing Values']
mz_table['% Total Zero Missing Values'] = 100 * mz_table['Total Zero Missing Values'] / len(df)
mz_table['Data Type'] = df.dtypes
mz_table = mz_table[
mz_table.iloc[:,1] != 0].sort_values(
'% of Total Values', ascending=False).round(1)
print ("Your selected dataframe has " + str(df.shape[1]) + " columns and " + str(df.shape[0]) + " Rows.\n"
"There are " + str(mz_table.shape[0]) +
" columns that have missing values.")
# mz_table.to_excel('D:/sampledata/missing_and_zero_values.xlsx', freeze_panes=(1,0), index = False)
return mz_table
missing_zero_values_table(results)
Output
Your selected dataframe has 6 columns and 5 Rows.
There are 6 columns that have missing values.
Zero Values Missing Values % of Total Values Total Zero Missing Values % Total Zero Missing Values Data Type
last_name 0 2 40.0 2 40.0 object
Test2_Score 2 2 40.0 4 80.0 float64
first_name 0 1 20.0 1 20.0 object
age 0 1 20.0 1 20.0 float64
sex 0 1 20.0 1 20.0 object
Test1_Score 3 1 20.0 4 80.0 float64
If you want to keep it simple then you can use following function to get missing values in %
def missing(dff):
print (round((dff.isnull().sum() * 100/ len(dff)),2).sort_values(ascending=False))
missing(results)
Test2_Score 40.0
last_name 40.0
Test1_Score 20.0
sex 20.0
age 20.0
first_name 20.0
dtype: float64
One-liner
I'm wondering nobody takes advantage of the size and count? It seems the shortest (and probably fastest) way to do it.
df.apply(lambda x: 1-(x.count()/x.size))
Resulting in:
Ord_id 0.000000
Prod_id 0.000000
Ship_id 0.000000
Cust_id 0.000000
Sales 0.002381
Discount 0.006548
Order_Quantity 0.006548
Profit 0.006548
Shipping_Cost 0.006548
Product_Base_Margin 0.012978
dtype: float64
If you find any reason why this is not a good way, please comment
If there are multiple dataframe below is the function to calculate number of missing value in each column with percentage
def miss_data(df):
x = ['column_name','missing_data', 'missing_in_percentage']
missing_data = pd.DataFrame(columns=x)
columns = df.columns
for col in columns:
icolumn_name = col
imissing_data = df[col].isnull().sum()
imissing_in_percentage = (df[col].isnull().sum()/df[col].shape[0])*100
missing_data.loc[len(missing_data)] = [icolumn_name, imissing_data, imissing_in_percentage]
print(missing_data)
By this following code, you can get the corresponding percentage values from every columns. Just switch the name train_data with df, in case of yours.
Input:
In [1]:
all_data_na = (train_data.isnull().sum() / len(train_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
missing_data.head(20)
Output :
Out[1]:
Missing Ratio
left_eyebrow_outer_end_x 68.435239
left_eyebrow_outer_end_y 68.435239
right_eyebrow_outer_end_y 68.279189
right_eyebrow_outer_end_x 68.279189
left_eye_outer_corner_x 67.839410
left_eye_outer_corner_y 67.839410
right_eye_inner_corner_x 67.825223
right_eye_inner_corner_y 67.825223
right_eye_outer_corner_x 67.825223
right_eye_outer_corner_y 67.825223
mouth_left_corner_y 67.811037
mouth_left_corner_x 67.811037
left_eyebrow_inner_end_x 67.796851
left_eyebrow_inner_end_y 67.796851
right_eyebrow_inner_end_y 67.796851
mouth_right_corner_x 67.796851
mouth_right_corner_y 67.796851
right_eyebrow_inner_end_x 67.796851
left_eye_inner_corner_x 67.782664
left_eye_inner_corner_y 67.782664
For me I did it like that :
def missing_percent(df):
# Total missing values
mis_val = df.isnull().sum()
# Percentage of missing values
mis_percent = 100 * df.isnull().sum() / len(df)
# Make a table with the results
mis_table = pd.concat([mis_val, mis_percent], axis=1)
# Rename the columns
mis_columns = mis_table.rename(
columns = {0 : 'Missing Values', 1 : 'Percent of Total Values'})
# Sort the table by percentage of missing descending
mis_columns = mis_columns[
mis_columns.iloc[:,1] != 0].sort_values(
'Percent of Total Values', ascending=False).round(2)
# Print some summary information
print ("Your selected dataframe has " + str(df.shape[1]) + " columns.\n"
"There are " + str(mis_columns.shape[0]) +
" columns that have missing values.")
# Return the dataframe with missing information
return mis_columns
Let's break down your ask
you want the percentage of missing value
it should be sorted in ascending order and the values to be rounded to 2 floating point
Explanation:
dhr[fill_cols].isnull().sum() - gives the total number of missing values column wise
dhr.shape[0] - gives the total number of rows
(dhr[fill_cols].isnull().sum()/dhr.shape[0]) - gives you a series with percentage as values and column names as index
since the output is a series you can round and sort based on the values
code:
(dhr[fill_cols].isnull().sum()/dhr.shape[0]).round(2).sort_values()
Reference:
sort, round
import numpy as np
import pandas as pd
df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0')
df.loc[np.isnan(df['Product_Base_Margin']),['Product_Base_Margin']]=df['Product_Base_Margin'].mean()
print(round(100*(df.isnull().sum()/len(df.index)), 2))
Try this solution
import pandas as pd
df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0')
print(round(100*(df.isnull().sum()/len(df.index)),2))
The best solution I have found - (Only shows the missing columns)
missing_values = [feature for feature in df.columns if df[feature].isnull().sum() > 1]
for feature in missing_values:
print(f"{feature} {np.round(df[feature].isnull().mean(), 4)}% missing values")
import pandas as pd
df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0')
df.isna().sum()
Output:
Ord_id 0
Prod_id 0
Ship_id 0
Cust_id 0
Sales 20
Discount 55
Order_Quantity 55
Profit 55
Shipping_Cost 55
Product_Base_Margin 109
dtype: int64
df.shape
Output: (8399, 10)
# for share [0; 1] of nan in each column
df.isna().sum() / df.shape[0]
Output:
Ord_id 0.0000
Prod_id 0.0000
Ship_id 0.0000
Cust_id 0.0000
Sales 0.0024 # (20 / 8399)
Discount 0.0065 # (55 / 8399)
Order_Quantity 0.0065 # (55 / 8399)
Profit 0.0065 # (55 / 8399)
Shipping_Cost 0.0065 # (55 / 8399)
Product_Base_Margin 0.0130 # (109 / 8399)
dtype: float64
# for percent [0; 100] of nan in each column
df.isna().sum() / (df.shape[0] / 100)
Output:
Ord_id 0.0000
Prod_id 0.0000
Ship_id 0.0000
Cust_id 0.0000
Sales 0.2381 # (20 / (8399 / 100))
Discount 0.6548 # (55 / (8399 / 100))
Order_Quantity 0.6548 # (55 / (8399 / 100))
Profit 0.6548 # (55 / (8399 / 100))
Shipping_Cost 0.6548 # (55 / (8399 / 100))
Product_Base_Margin 1.2978 # (109 / (8399 / 100))
dtype: float64
# for share [0; 1] of nan in dataframe
df.isna().sum() / (df.shape[0] * df.shape[1])
Output:
Ord_id 0.0000
Prod_id 0.0000
Ship_id 0.0000
Cust_id 0.0000
Sales 0.0002 # (20 / (8399 * 10))
Discount 0.0007 # (55 / (8399 * 10))
Order_Quantity 0.0007 # (55 / (8399 * 10))
Profit 0.0007 # (55 / (8399 * 10))
Shipping_Cost 0.0007 # (55 / (8399 * 10))
Product_Base_Margin 0.0013 # (109 / (8399 * 10))
dtype: float64
# for percent [0; 100] of nan in dataframe
df.isna().sum() / ((df.shape[0] * df.shape[1]) / 100)
Output:
Ord_id 0.0000
Prod_id 0.0000
Ship_id 0.0000
Cust_id 0.0000
Sales 0.0238 # (20 / ((8399 * 10) / 100))
Discount 0.0655 # (55 / ((8399 * 10) / 100))
Order_Quantity 0.0655 # (55 / ((8399 * 10) / 100))
Profit 0.0655 # (55 / ((8399 * 10) / 100))
Shipping_Cost 0.0655 # (55 / ((8399 * 10) / 100))
Product_Base_Margin 0.1298 # (109 / ((8399 * 10) / 100))
dtype: float64