计算 class 中某个属性的元素出现百分比。 Python
Counting percentage of element occurence from an attribute in a class. Python
我有一个名为 class 的交易具有这些属性
Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type,
location])
数据集的例子是这样的
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
有不同的交易类型。在 trans_type 中定义:
advance, balance, deposit, transfer, withdrawal
如何计算交易类型百分比?
例如,这将是结果列表:
[('advance', 20), ('balance', 20), ('deposit', 20), ('transfer', 0), ('withdrawal', 40)]
这是我试过的:
#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 += 1
elif trans_type == 'balance':
balance += 1
elif trans_type == 'deposit':
deposit += 1
elif trans_type == 'transfer':
transfer += 1
elif trans_type == 'withdrawal':
withdrawal += 1
使用 for element in range(len(atm_transaction_list)):
,您将迭代一个范围内的整数。这通常在您要使用索引时使用。但是,您没有这样做。只需使用 for transaction in atm_transaction_list:
遍历交易列表本身。每个 transaction
将成为一个 Transaction
对象。
我还建议将结果存储在字典中,而不是存储在五个单独的参考文献中。然后,您可以在看到某个键的值时添加它。
result = {'advance':0, 'balance':0, 'deposit':0, 'transfer':0, 'withdrawal':0}
for element in atm_transaction_list:
result[element.trans_type] += 1
这将为您提供一个字典,您可以使用 result['advance']
之类的内容访问该字典以查看 'advance'
笔交易的数量。
现在将每个键的值除以交易总数再乘以 100 得到百分比:
l = len(atm_transaction_list)
for key in result:
result[key] = result[key] / l * 100
我有一个名为 class 的交易具有这些属性
Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type,
location])
数据集的例子是这样的
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
有不同的交易类型。在 trans_type 中定义:
advance, balance, deposit, transfer, withdrawal
如何计算交易类型百分比?
例如,这将是结果列表:
[('advance', 20), ('balance', 20), ('deposit', 20), ('transfer', 0), ('withdrawal', 40)]
这是我试过的:
#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 += 1
elif trans_type == 'balance':
balance += 1
elif trans_type == 'deposit':
deposit += 1
elif trans_type == 'transfer':
transfer += 1
elif trans_type == 'withdrawal':
withdrawal += 1
使用 for element in range(len(atm_transaction_list)):
,您将迭代一个范围内的整数。这通常在您要使用索引时使用。但是,您没有这样做。只需使用 for transaction in atm_transaction_list:
遍历交易列表本身。每个 transaction
将成为一个 Transaction
对象。
我还建议将结果存储在字典中,而不是存储在五个单独的参考文献中。然后,您可以在看到某个键的值时添加它。
result = {'advance':0, 'balance':0, 'deposit':0, 'transfer':0, 'withdrawal':0}
for element in atm_transaction_list:
result[element.trans_type] += 1
这将为您提供一个字典,您可以使用 result['advance']
之类的内容访问该字典以查看 'advance'
笔交易的数量。
现在将每个键的值除以交易总数再乘以 100 得到百分比:
l = len(atm_transaction_list)
for key in result:
result[key] = result[key] / l * 100