Analyzing Expense Data with Python: A Step-by-Step Guide
School
Cornell University**We aren't endorsed by this school
Course
CS 1110
Subject
Accounting
Date
Dec 12, 2024
Pages
12
Uploaded by DeanSquidMaster1220
Python 3.12.1 (v3.12.1:2305cas144, Dec 7 2023, 17:23:38) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license()" for more information. >>> inport numpy as np >>> inport pandas s pd Warning (from warnings module): File "<pyshell#1l>", line 1 Deprecationarning: Pyarrow will becone a required dependency of pandas in the next major release of pandas (pandas 3.0), (to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries) but was not found to be installed on your system If this would cause problems for you, please provide us feedback at https://github,con/pandas-dev/pandas/issues/54466 >>> inport matplotlib.pyplot as plt >>> file_path = '/Users/leyasan/Downloads/FY2021_Proposed_Expense_Budget.csv' >»> data = pd. read_csv(file_path) >>> print(“Column headings: Column headings: > print (data. colunns) Tndex(['Fiscal Year', 'Service', 'Department', 'Progran', 'Expense Category', *Reconmended Amount', 'Approved Amount', 'Actual Amount', 'Fund’, *Fund Type', 'Expense Type'l, dtype="object’) >>> print(data. colunns) Index(['Fiscal Year', 'Service', 'Department', ‘Progran’, 'Expense Category', *Reconmended Amount’, 'Approved Amount', 'Actual Amount', 'Fund’, *Fund Type', 'Expense Type'l, dtype="object’) >>> actual_data = dataldatal'Expense Type'] == 'Actual'] >>> expense_type_totals = actual_data,groupby('Expense Category') ['Actual Anount'l.sun() >>> main_expense_types = expense_type_totals.nlargest(5) >>> print("Hain expense types. Main expense types: 5> print (nain_expense_types) Series([], Name: Actual Amount, dtype: floatsa) >»> main_expense_data = actual_datafactual_datal Expense Category'l.isin(main_expense_types. index)] 55> main_expense_totals = main_expense_data.groupby (' Expense Category')['Actual Anount'].sum() >>> print("Total spending for each main expense type:" Total spending for each main expense type: >>> print(nain_expense_totals) Series([], Name: Actual Amount, dtype: float6d) >>> main_expense_types = datal'Expénse Type'l.unique() >>> print("Main expense types:") Main expense types: 53> for expense_type | main_expense_types: print (expense_type) salaries Capital Enployee Benefits Services Supplies >>> main_expense_types = ["Salaries”, "Capital”, “Employee Benefits", "Services”, "Supplies"] >>> Filtered_data = dataldatal Expente Type'],isin(nain_expense_types)] >>> expense_type_totals = filtered_data.aroupby( Expense Type')['Actual Amount’l.sun()
555 print(expense_type_totals) Expense Type Capital 16126960+ Enployee Benefits 1.898083e/00 Saleries 9.161591e408 Services 8.4972500408 Suppies a.7637510007 Nane: Actual Arount, divpe: float6s »>> salaries_dats = data (datal Expense Type'] == ‘Salaries'] 55> salaries by departeent = salaries_data.groupby( Departnent')['Actual Asount ].sun() 22> top_departnents = solaries_by_departnent.nlargest(2) 355 print(*Departnents with the nighest total spending on sataries Departnents with the highest total spending on salaries: 55 fo departnent, totsl_spending .1 top_departnents.itens(): print(t" (departnent): (total_spending)”) Fire (101-303): 313301804.0 Police (101-302); 207619962.76
> total_spending_police = 297619902.76 - toteT salaries Spending = sataries datal tctuat ot ] sual) > print{-Total spending on sata ontne entice datasel ", total salaries_spending) Total spanding on setaries. fron the entire aatssets I16159051. 1300061 > total_salaries_spending = 916159051, 1300001 5> totalspending fire = 313301844.4 > Traction fire = total_spending fire / total salaries_spending 2 fraction police = total_spendino_police / total saleries. spending o> prANT("FTaCtion of {0taT Spending on salsries rETescnted by <ach departaent:®) Fraction of total spending on salaries represented by each departmen 5> froction._fire salaries = fotal_spending_fire / total salaries. spending > fraction_police_soaries = total_spending_police / totol_salories spending S print (7 . S Firebepartntat s Fraction Tire. sataries) Fraction of solories spent by’ Fire separtaent: 0. 34197520215005154 22> print("Fra action_police_salories) Fraction of salaries spent by olice ueu.\unc«\ 2. 32aas61506701975 5> expense cateaories = datal 't “oory 1. uniauel) e
year act_amount o1 18028.03 e 266201 500,19 200 16208.21 010 a4e7s.18 iisss 2021 Tiesn 2021 151 2021 11852 2021 11853 2018 (11854 rous x & caluans] pd.options . display, float_format = *(:.17)" . format df-groupby{['yer 1) ['act_anount 1.nean() 2012 3089810 359547 Name: act_amount, dtype: floated af-groupby (1 'yesr 1) [ epp_snount 1.nean() year 37132 2021 0.0 Nane: app_anount, dtype: floated df-groupby (1 year 1) [ rec_snount].nean() year 3291611 e 36772118 354619 3791283 37953405 396121.0 8772 s33503.8 5346445 Rame: rec_amount, dtype: floated st data = data. Loc(stationery_indices] ({'year ' 1datal’Fiscal Year ], act datal'Actual Anount'], " rec_amount’: dotal'Recommendcd A datal’ Actual Amount'] " rec_amount': datal'Recommended Anount'1,’app-anount nt', "app_amount tdatalApproved Anount’13) atal*Approved Anaunt'T})
st_data < data, locstationery_indices] Traceback (nost recent call last) File “epyonel1ssbon, Line 1, in <module> 5% _dats - gata. Lo (stationery_indices] NameError: nane 'stationery_indicss’ is not defined Siationery_occurrences = @ata.apply(|/0i- row: row.astype(str).str.contains( Stationery’, casest Stationery_indices = stationery.occurrences [stationery_occurrences] . index stationery indices Tndex([ 2335, 2651, 10489, 10490, 10491, 10493, 10494, 10496, 10497, 10500, “eeany(), axise1) ii792, 11001, 11804, 11807, 11815, 11820, 11824, 11829, 11838, 11850, dtype=’int6d’, Length=369) st_data * data. loc (Stationery_indices] brint(st datal i cal vear Service Fund Type Expense Type s 2013 public Safety Tax Supported Supplies 2651 2020 City Courts Tox Spported Supplies. 12 city couneil Tox Supported Supplies. 2 City Council Tox spported Supplies W2 city couneit Tox Supported Supplies 2631 public Safeiy Tox supported Supplies 2021 public Safety Tox Supported Supplies 2021 Public Safety ... Tox Supported Supplies 2021 public satety Tox Supported Supplies 2021 Recreation ... Tax Supported Supplies (369 rows x 11 coluans] df = pd.Oatarame({ year prine(at) alFiscal Year'], ‘act_smount s st tal'Actual Amount 1, ' rec_smount 1 st_da [ Recasmended Anount’1, act_amount rec_amount app_mount T2000.0 0.0 9989.9 X 9453 o T2000.0 0.0 15000 a ole Tiese 2021 10500 oo 1369 rows x 4 coluens] pd.options.display. float_forsat = '(:.1)".format f.groupby{ ['year 1)1 act_anount 1. mean() ye anes. prirn Nane: act_smount, dtype: floatéd af-groupby (1'year D' rec_anount’].mean() sop_amount _datalApproved Anount 1) Lni 117 Cole 16,
(369 rous x 11 cotumns] >> df = pa.DataFrane((’jear 55 prantlan year act_smount rec_amount app_smoun a5 Zels | 9989, Taono 12000, o . o 1850 201 10500 1369 rows x 4 colums] > pd.options . display. Tloat_fornat = *(:.1)',format 222 df groupby( ['year 1) 1'act_saount].mean( yea 2012 3 2013 ls 2014 X1 205 s 26 el 207 X1 frird ia 2015 i ] 21 e Nane: act_amount, diype: floatss 525 dt.groupby ([ year )" rec_snount'1.mean() year 2 sanve s 67zl a4 assle fas sean.0 e 4923 W amaz mie aazely s s mze asiiy 821 aa3as ane: rec_amaunt, dtype: Tloated >»> df.groupby(L'yeal 1) | asp_anoun"].mean() year B eeso a3 e7aan i s seas.n 206 1992.3 w7 018 aa79.1 w9 mnls w0 15117 0 Nane: app_shount, dtype: floatet 7 cateqary 1 expense_categories: brinticategory) HOLIOAY (50340) VACATION (50090) SICK LEAVE (50080) SALARIES (50010) STRUCTURES & THPROVENENTS (52006) € datal'Fiscal Year'], "act_amount': st_datal'Actuat Amount'], *rec_asount + st_datal ‘Recommended Asourt"], 'app_amount”:st_datal'Approved Amsunt 1}
5> plt.zaverigl nian 555 Dl natptoriib pyptor e xpense.covegarics o datartiels, 41:urtduet] 35 print{expense_categories) HOLIDAY (50349)" "VACATION (50998) " SICK LEAVE. (50 1SALARIES (50010 'STRUCTURES & INPROVENENTS (52685) INISC PROJECT EXPENSE (52883)" 'CONSTRUCTION (52882)" {CITY OF PROVIDENCE PENSION EXPENSE (51850)' 'DENTAL INSURAICE (51570)" 'F.T.C.A. (51950) "LASORERS INT'L PENSION (51820)" 1LOGAL 1333 BENEFITS EXPENSE (51959) |TRANSFER T0 ACTIVE NEDICAL TN FUND (59891)' 'AUTO ALLOWANCE (51993)" |HEALTHCARE EE CASH PAYWENT (51582)' 'TUITION (52705 VELECTED OFFICALS PENSION CONTRIBUTION (51840) " {ENPLOVEE DEATH BENEFITS (51662)! 'STATE UNEWPLOY CONP (51978} *TRANSFER 10 RETIREE MEDICAL INS FUND (59892)" {ADDTIONAL FENSTON REFORA SAVINGS (59101 IIURY PAWKENTS (51925)° INEDICAL SERVICES 71 DUES & SUBSCRIPTIONS mwsv' w»z»ns Glsse |TRANSFER TO SCHOOL CO. EDUCATIONAL TNCENTIVE SALARTES. (31607)* P LEGAL Funo (519351 {LEGAL SERVICE (51030) 'POLICE LEGAL FUND (51910; TRAMSFER To RALNY DAY REGERIE FIND (s9653) |~ "POSTAGE (521700 ‘WISC, EXPENSES (53580) ' "DEFICIT REDUCTION (5900 *TRANSFER TO PUBLIC SAFETY INSPECTION (59665)' ‘PRINTING (52219) 1633 DENTAL [51572)" 'TRAVEL (53105)" “TRANSFER T0 FUND 245 (59245) " {RETIREE MEDICAL (51520)! 'STATIONERY (50020) |TRANSFER TO FUND 351 (59351)" 'TRANSFER TO FUND 231 (50231)" |TRANSFER TO FIND 816 (59816)" 'TIRES (54805)’ 'FO0D (34725)" |TRAISFER T0 820 (59529)° *TRANSFER TO WARY E SHARP TREE FUND 676 (S9676)° 'FUEL (54730)" |UNDESIGNATED SAVINGS [PENSION) (51851)" *LONGEVITY (50350]" 1CALL BACK (50180)" OVERTIME (50060)' *SALARIES-TEHPORARIES (50028) " 'WITNESS FEE (50370)' 'ALTERNATIVE WORK WEEK (50405) {DIFFERENTIAL PAY (50400)' "POLICE & FIRE DETAILS FOR CITY DEPTS (50063)" 1SHOW REHOUAL OVERTINE (S0130) 'ELECTION OFFICIALS (50150)" {HOLIDAY PAY/ND RETIRENENT (50331) 'SERVICE OUT OF RANK (50110 {HOLIDAY PAY POLICE FIRE (5033) 'INJURED EWPLOYEES (30250)' "NON-UNION SEVERANCE (50610)° 'PROVISION FOR EXTRA PAY PERIOD (50095)° {OVERTINE - NESTEP PROGRAN (50062)' “OVERTINE - SPECIAL EVENTS (50965 " 1PRIOR RET SICK SEVERACE INSTALL. FT (50082) " {PRIOR RET VACKTION SEVERANCE INSTALL, PHT (59095)" {OVERTINE - TRAINING (50966)* *FIRE CALLGACK CONTINGENCY (50185) 1SICK LEAVE BOWUS (50081)' "HOUSEKEEPING SUPPLIES (5474 TRAIN & SERVICE (529111 'TRANSPORTA FEES NOT CLASSIFIED (52120)° 'CONTINGENCIES (35980)" ThsrER < COCLL coNTIRGENCY 4 {FISCAL AGENTS FEES (52205]" "REPAIRS TO OFFICE EQUIPNENT (52900) {OFFICIAL BOND (55139)' 'COPIERS LEASE AND MAINTENANCE (52013)" {RED LIGHT CAMERA FEES (53430)° 'WITNESS FEES (52115 {PRIVATE CONTRACTORS (3491)° *COURT REPORTERS (53307)" {REPATRS TO AUTOS & TRUCKS (52012) " {RENTAL OF LAND AND SUTLDINGS (53000) ' "GARBAGE PICK UP (53402)" {REFUSE PROCESSING (52028)" ‘RECYCLING PICK Up (52350)¢ {STREET SWEEPING (52345) " "WASTE RECYCLING EDUCATION (52385)" REPAIRS TO OTHER EQUIPHENT (52930)® ‘RENTALS MO CLASSIFIED (3011)° {REPATRS TO EQUIPHENT (52905)" 'BANK SERVICE CHARGES (52500) |TELLER SHORTAGE/OVERAGE (52505)" 'AUCTIONEERS SERVICES (53326)" |EXANINING TITLES (53325) ' CONTRACTUAL SERVICES/TRAINING (53425)" {RENTAL OF EQUIPWENT (53010)" 'CEREMONIES (55110 |GRANT EXPENDITURES (53125)" "HEAT & LIGHT & POWER (52430) {RESERVE OF ANTIC ABATEWENTS (SS144)' “WEDICAL FEES (53300) |CONTRACTUAL SERVICES/OTHER SERVICES (53499)" 'TRANSPORTATION (52220)° | INFORAATION TECHNOLOGY (53440)" 'DATA PROC DIV CHARGES (52040} " {PAY OF CLAIMS & DAWAGES (52152) {REPATRS GFFICE WACK. FURNITURE (52930)° DEBT SERVICE — INTEREST (52201)' 'DEDT SERVICE — PRINCIPAL (52200) VINTEREST PAYHENTS - MASTER LEASES (52203 {PRINCIPAL PAYMENTS — WASTER LEASES (522021 420 Col:0
78 2021 Recreation ... Tax Supported salaries (611 rows x 11 coluans] 555 departments = salaries.data[‘Departnent uniaue() 22 print(departments) [arcnives (101-913)" rcity Clerk (101-103)' 1City Council Adainistration (101-910]" "City Council Members (101 10ffice of the Internal Auditor (161-911)° ‘Treasury (101-209)' “Housing Court (101-110)" ‘Municipal Court (181-105)" ‘Prabate Court (101-107)' Bldg Inspect Code Enforcement (101-416)" 18dg Inspect ion Prosecotion (101 413)° "Building Adninistration (101-401)' 'Building Soard (101-407)" ‘Electrical Tnstallotion (101-404) "Hechanical EquipTnstallation (101-405)" *Plusbing Drainage & Gas Piping (101-403)" 1Structures & Zoning (101-402)° 'Zoning Board of Review (191-496)" ‘€ngineering & Sanitation (101-502)' "Environmental Control (101-506)" [Garage ReN Equipsent (101-515) “Highuay. (101-508)" 1Parking Adninistration (191-516)" 1PUBLLC Works Adninistration (101-501)° ‘Sewer Canstruction (101-511)° 1Snow Resoval (101-510)° “Traffic Engineering (101305 iBgara of Tax esesspercanevles T1e1208]" Teiey coliccror (1032050 1City Controller (191-202)' City Tax Assessor (1012 *Finance (101-201)" 'Recorder of Deeds (101-991) Retiresent Office (101-203)+ “Non Departnental-General Fund (101 ARt tare, Fita: § Toutisn 1015160 avsing Aetheriey (10113091 “Hunah Relations (101-008)" ‘Human Services (101-017)' 'PERA (101-1311)" ‘Data Processing (181-204): *Law Departaent (101-105) 1City Sergeant (101-104)" "Mayor's Dfice (101-101 ‘Board of Canvassers (1810041 ‘Bureau of Licenses (161-905)" ‘Vital Statistics (101-993)" "Botanical / Greenhouse (101-707)' ‘Forestry Services (101-703)" Nelghborhood Park Services (181-762)' "North Burial Ground (101-710)" ‘Reger Williams Park Services (101-708) ISuperintendent of Parks (101-709) ' ‘Zoological Services. (10 ‘Persomnel (181-212)" 'Planning & Urban Developaent (101-008)" "Public Property (101-1801)" "Connissloner of Public Safety (101-301)" IConmunications (101-304) ! Energency Hgat / Honeland Sec,. (191-907)" 1Fire (101-303) "Police (101-302) ‘Recreation Seasonal (101-802) "Recreation (181-601) “0ffice of Econoaic Opportunity (101909 {Porking Adninistration (101-938)'1 27 departmeres 61 = pitarrane(depnrinert s depriaents)) ="Pd.nerge(salaries_data, departnents_df, on='Departnent’, howsle1t) S e rics et cepartnents] Flscal Year Service 202 Fund Type Expense Type City Council ... Tax Supported = Salaries City Council ... Tex Supported Salaries City Council ... Tax Supported Salaries city Councit Tox Supported Salaries City Councit Tox Supported Sataries 2001 pubic satety Tax supported sataries 3021 punlic Satety Tax Supported Sataries 2001 punlic Satery Tox Supported Salaries 2001 punlic Sacery Tax Supported Sataries 2001 Recreation Tax Supported Sataries o) File "<pyshel1#1075", Line 1, in <nodule inport seaborn 23 ins HoduleNot FoundE rror: No nodule naned *seaborn’ > pip nstall seaborn Syntaxkrror: inv1ld syntax 5> plt. flgure(figsize=(12, 6)) SFigure size 2400x1200 with 0 Axess 22> Borplot = olarics wltn deparinents.boxplot(columne Seconsenies ARouns, by='oepartac 222 Doxplot. et ylabel( Recammended Anc TR P05 oUT MR 443, .. Recommende Anount) . figsizes(1s, 8), rot=s0) 420 Col:0
‘Structures & Zoning (101-402)° Zning Board of Raview (101-406)" ‘Engincering & Sanitation (101-502)" "Environmental Control (101-506)" “Garage RS Equipeent (181-515)" 'Highkay (101-508)" “Parking Adninistration (101-516)" Public Works Administration (101-591)' 'Sewer Construction (181-511)" ‘S0 Resoval (101510] ' 'Traffsc Engineering (101305 1Board of Tax AssessmentGeview (161-208)' 'City Collector (101-205)" 1City Controller (181-202)' 'City Tax Assessor (161-207)" ‘Finance (101-201)" 'Recorder of Deeds (101-991)" "Retireaent Office (101-203)' Non Departnental-General Fund (101 ‘AFts, Culture, Fila, & Tourdsn (101-916)" *Housing Authority (101 "Hunan Relations (181-098)' Hunan Services (101-917)' 'PERA’ (101 *Low Departaent (101-105)" "Mayor's 0ffice (101-101)" “Board of Canvassers (101-904)° ‘Bureau of Licenses (11-995)" 1Vital Statistics (101-903]° ‘Botanical / Greenhause (101-707)' {Forestry Services (191-703) ‘Nelghborhood Park Services (191-702) INorth Burial Ground (101-710)" 'Roger Willians Park Services (101} 1SUperintendent of Parks (101-709)° ‘Zoological Services (101-706)1 persomet (1612121 'Plannie & yrban Developnent. (161-908)" public praperty (Gos-saet “Comiseloner ef Pblic sereey Gionaen) Conmunications (101-304) *Energency Hant / oneland Sec. (101-907] ‘Fire (101-303) “Police (101-302)° ‘Recreation Seasonal (101 “Recreation (101601} ‘0ffice of Economic Opportunity (191-809) ‘Parking Adninistration (101918)'1 >»> departnents df = pd.bataFrame(( epsrirent s departaents)) 27 CESriec Wit deparinents - po.nerge(salaries_iata, departnents_df, ons'eparcment’, hows'left’) 35 print(salaries with_departaents) Fiacat vear Service . Fund Type Expense Type o city Council Tax Supported Sataries 1 city Couneat Tax Supported Sataries 2 city Councit Tax Supported Sataries 3 Gty Council Tax Supported Selaries 3 City Council Tax Supported Solaries o6 Public satety Tax swported Salares 507 PUblic Safety |1 Tax Supported Salaries 608 PUbLLC Safety L. Tox Supported Selaries 505 Public Safety Tox Supported Solaries 610 Recreation || Tax Supported Solaries {611 roes < 11 coumat R thost recent catl lsst) pyehe 1107, Tine 1, in <sodute- iiort seaborn 40 WouoHotFoumdE ror: o odute named *sesborn 5> i Erstatt. seaorn B RemmErrors invatia syntac sos . flpurelrigeize=(12, 611 Zsird " Tian Saaverznn with o pes> 2o» oxblot = su1aries ith Sapartomnce.boxplat (columne'Recommendad Anount*, bys'Departaant’, figsizentid, 8), rotedt) 323 Dorbtat sty abell Recovmcased Ameint.] T (205 GG HaakiAsss, 0.5, Recommenied Anount:) 25 GraupeL iata o Sr1ariea-uiin depariments-arochor [ Fiscal Year", ‘Departasnt!])['Necompendad Amount'] (. raset_tadex() 333 plt.rigure(rigsiz ) Curd i Theeeaoas uith 0 mcess 2= boxplot = grouped.data. boxplot (colunr Fiscal vesr!, figsize=(14, B), rot=90) 52> plt.title(*Distribution of Recomended Dudget Sizes for Salaries by Departnert and Fiscal Year Text(0.5, 1.0, ‘Distribution of Recomnended Budget Sizes for Salaries by Department and Fiscal Year') s> pltoxtabet(‘Phecat vonr Roxt (0.5, 138.69444434444446, 'Fiscal vear') »» plt.ylabé1( Recannenied Anount') Texitans gossssasdsesss, .5, “Recomended smount Lt yiale, araiped_dotal fecommences Ao 1 ahantite(e.o5)) Tors! Toavist. ey 52> pi.Showt) 420 Col:0
Mean Amount 80000 60000 40000 20000 Mean F.I.C.A Taxes Year-to-Year T T —e— Mean Actual Amount —e— Mean Approved Amount —e— Mean Recommended Amount 2012 2013 2014 2015 2016 2017 Fiscal Year 2018 2019 2020 2021