尝试在列表中获取总和以在输出中打印出字符串

Trying to get sums in lists to print out with strings in output

我一直在进行一个 Python 项目分析 CSV 文件,但无法得到输出以显示我的字符串总和,只能列出应求和的数字。

我正在使用的代码:

import pandas as pd

data = pd.read_csv('XML_projectB.csv')
#inserted column headers since the raw data doesn't have any
data.columns = ['name','email','category','amount','date']

data['date'] = pd.to_datetime(data['date'])

#Calculate the total budget by cateogry
category_wise = data.groupby('category').agg({'amount':['sum']})
category_wise.reset_index(inplace=True)
category_wise.columns = ['category','total_budget']

#Determine which budget category people spent the most money in
max_budget = category_wise[category_wise['total_budget']==max(category_wise['total_budget'])]['category'].to_list()

#Tally the total amounts for each year-month (e.g., 2017-05)
months_wise = data.groupby([data.date.dt.year, data.date.dt.month])['amount'].sum()
months_wise = pd.DataFrame(months_wise)
months_wise.index.names = ['year','month']
months_wise.reset_index(inplace=True)

#Determine which person(s) spent the most money on a single item.
person = data[data['amount'] == max(data['amount'])]['name'].to_list()

#Tells user in Shell that text file is ready
print("Check your folder!")

#Get all this info into a text file
tfile = open('output.txt','a')

tfile.write(category_wise.to_string())
tfile.write("\n\n")
tfile.write("The type with most budget is " + str(max_budget) + " and the value for the same is " + str(max(category_wise['total_budget'])))
tfile.write("\n\n")
tfile.write(months_wise.to_string())
tfile.write("\n\n")
tfile.write("The person who spent most on a single item is " + str(person) + " and he/she spent " + str(max(data['amount'])))
tfile.close()

CSV 原始数据如下所示(将近 1000 行):

Walker Gore,wgore8i@irs.gov,Music,.98,2017-08-25
Catriona Driussi,cdriussi8j@github.com,Garden,.35,2016-12-23
Barbara-anne Cawsey,bcawsey8k@tripod.com,Health,.38,2016-10-16
Henryetta Hillett,hhillett8l@pagesperso-orange.fr,Electronics,.52,2017-03-20
Boyce Andreou,bandreou8m@walmart.com,Jewelery,.77,2016-10-19

我在 txt 文件中的输出如下所示:

      category               total_budget                                                                                                                                                                                                                                                                                                                               
0    Automotive              .04.99.66.32.07.91.40.28.41
1          Baby              .14.59.50.86.99.55.74.63.65
2        Beauty              .67.95.64.25.53.25.42.77.74
3         Books              .03.68.21.43.17.96.81.33.80
4      Clothing              .07.29.23.78.50.81.36.80.90

    year  month               amount                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
0   2016      9               .95.81.64                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
1   2016     10               .14.08.51.15.31.24.83.22.72                                                                                                               
2   2016     11               .22.00.86.14.13.82.81.83                                                                                                                                                                                                                                              
3   2016     12               .32.93.95.41.65.69.26.53                       

我希望 total_budget 列是每个类别列表的总和,而不是您在此处看到的各个值。 months_wise 也是同样的问题,它给了我单独的值,而不是总和。 我尝试了写入行中的 {} .format、.apply(str)、.format 本身,以及几乎所有其他 Python 从我能想到的列表中转换为字符串的排列,但我我难住了。

我在这里错过了什么?

正如@Barmar 所说,来源有 $XX 所以它不被视为数字。您可以尝试按照 this 方法将值解析为 integers/floats 而不是其中包含 $ 的字符串。