Counting percentage of element occurence from an attribute in a class. Python

2024/11/16 7:49:41

I have a class called transaction that have these attributes

Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type,
location])

an example of the data set is as such

timestamp   time    date    weekday duration    amount  trans_type      location
1           0:07    3       thu      2                  balance         driveup
2           0:07    3       thu      6          20      withdrawal      campus a
3           0:20    1       tue      2          357     advance         driveup
4           0:26    3       thu      2          20      withdrawal      campus b
5           0:26    4       fri      2          35       deposit            driveup

There are different transaction types. define in trans_type which are:

advance, balance, deposit, transfer, withdrawal 

How do I calculate the percentage types of transaction?

For example, this will be the resulting list:

[('advance', 20), ('balance', 20), ('deposit', 20), ('transfer', 0), ('withdrawal', 40)]

This is what i have tried:

#percentage of the different types of transactions
advance = 0
balance = 0
deposit = 0
transfer = 0
withdrawal = 0
for element in range(len(atm_transaction_list)):for trans_type in element:if trans_type == 'advance':advance += 1elif trans_type == 'balance':balance += 1elif trans_type == 'deposit':deposit += 1elif trans_type == 'transfer':transfer += 1elif trans_type == 'withdrawal':withdrawal += 1
Answer

With for element in range(len(atm_transaction_list)):, you are iterating over integers in a range. This is generally used when you want to work with indices. However, you're not doing that. Simply iterate over the transaction list itself, with for transaction in atm_transaction_list:. Each transaction will then be a Transaction object.

I would also recommend storing your results in a dictionary instead of in five separate references. You can then add to a key's value whenever it's seen.

result = {'advance':0, 'balance':0, 'deposit':0, 'transfer':0, 'withdrawal':0}
for element in atm_transaction_list:result[element.trans_type] += 1

This will give you a dictionary that you can access with something like result['advance'] to see the number of 'advance' transactions.

Now divide each key's value by the total number of transactions and multiply by 100 to get the percentage:

l = len(atm_transaction_list)
for key in result:result[key] = result[key] / l * 100
https://en.xdnf.cn/q/120348.html

Related Q&A

AWS | Syntax error in module: invalid syntax

I have created python script which is uploaded as a zip file in AWS Lambda function with stompy libraries bundled in them.Logs for python 2.7:-Response: nullRequest ID: "c334839f-ee46-11e8-8970-61…

Algorithm for finding if an array is balanced [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Merging two dataframes in python pandas [duplicate]

This question already has answers here:Pandas Merging 101(8 answers)Closed 5 years ago.I have a dataframe A:a 1 a 2 b 1 b 2Another dataframe B:a 3 a 4 b 3I want my result dataframe to be like a 1 3 a …

Searching for the best fit price for multiple customers [duplicate]

This question already has an answer here:Comparing multiple price options for many customers algorithmically(1 answer)Closed 10 years ago.A restatement of Comparing multiple price options for many cust…

Can we chain the ternary operator in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.The com…

evaluate a python string expression using dictionary values

I am parsing a text file which contain python "string" inside it. For e.g.:my_home1 in houses.split(,) and 2018 in iphone.split(,) and 14 < maskfor the example above, I wrote a possible di…

How to simply get the master volume of Windows in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 1 year ago.Improve …

Python: Adding positive values in a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python IM Program [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Changes in Pandas DataFrames dont preserved after end of for loop

I have a list of Pandas DataFrames and I want to perform some operations on them. To be more precise, I want to clean their names and add new column. So I have written the following code:import numpy a…