如何使用值计数实例并将它们乘以整数来生成值?

2024-05-28 18:31:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一家运输公司工作,开帐单。我想能够自动化我的excel表,以便我导入一个excel表,它为我做帐单

  • 我知道如何导入excel表格
  • 我知道如何使用.value\u counts()函数来计算计费服务的出现次数
  • 我不知道如何取这些值并乘以它们来产生服务费

我已经为我的excel工作表生成的内容创建了一个模拟数据框架。我只是不知道如何利用价值计算产生的价值,自动为我计算服务费

d = ['wheelchair', 'ambulatory', 'wheelchair', 'ambulatory','wheelchair', 'ambulatory', 'wheelchair', 'ambulatory']
df = DataFrame(data = d, columns = ['Device'])
  Device
0  wheelchair
1  ambulatory
2  wheelchair
3  ambulatory
4  wheelchair
5  ambulatory
6  wheelchair
7  ambulatory

typeoftransport = df['Device'].value_counts()
typeoftransport
ambulatory    4
wheelchair    4

我想让它生产这个

ambulatory, has 4 rides, charge with fee 8.00$, total fee for service $32.00
wheelchair, has 4 rides, charge with fee 20.00$, total fee for service $80.00

Tags: dfvaluedeviceexcelhas价值ridescounts
2条回答
mapping = {"ambulatory":8,"wheelchair":10}
v_c = df['Device'].value_counts()
for i, j in zip(v_c.index, v_c.values):
    print("{}, has {} rides, charge with fee {}, total fee is {}".format(i, j, mapping[i],mapping[i] * j))

欢迎来到Stack!以下内容可以帮助您

from collections import Counter

d = ['wheelchair', 'ambulatory', 'wheelchair', 'ambulatory','wheelchair', 'ambulatory', 'wheelchair', 'ambulatory']

price = {'wheelchair' : 8, 'ambulatory' : 20} ##you've to assign price of every item in your database

c = Counter(d)

for key, value in c.items():
    print(f"{key}, has {value} rides, charge with a fee of {price[key]}$, total fee for service {price[key]*value}$")

希望有帮助。安静下来

相关问题 更多 >

    热门问题