我的任务是创建一个Python脚本,该脚本分析pybank.csv中的记录以计算以下各项: https://i.stack.imgur.com/MzM4N.png
数据集中包含的总月数 整个期间的“利润/亏损”净额总计 整个期间“利润/亏损”变化的平均值 整个时期内利润(日期和金额)的最大增长 在整个期间内,损失(日期和金额)的减少幅度最大
Expected Results:
Financial Analysis
----------------------------
Total Months: 86
Total: $38382578
Average Change: $-2315.12
Greatest Increase in Profits: Feb-2012 ($1926159)
Greatest Decrease in Profits: Sep-2013 ($-2196167)
我无法添加具体日期。
我尝试了嵌套循环,numpy库和儿子...
import statistics
import numpy as np
df = pd.read_csv("./pybank.csv")
f = open('results.txt','w+')
numMonths = df['Date'].count()
profit = df['Profit/Losses'].sum()
increaseProfit = df['Profit/Losses'].max()
profitLossesList = list(df['Profit/Losses'])
dateProfitLossesList = df['Date']
changeList = []
counter = 0
numDB = np.array(profitLossesList)
dateNp = np.array(dateProfitLossesList)
date = ""
for i in range(len(df)-1):
curr = profitLossesList[i]
currDown = profitLossesList[i+1]
changeList.append(currDown-curr)
counter = sum(df['Profit/Losses'])
minChange = min(changeList)
maxChange = max(changeList)
avgChance = round(np.mean(changeList),2)
#Escribimos en el documento results.txt los resultados
f.write(f"Financial Analysis\n----------------------------------\n")
f.write(f"Your profit is: ${profit}\n")
f.write(f"Total Months {numMonths}\n")
f.write(f"Average Change: ${avgChance}\n")
f.write(f"Greatest Increase in Profits: (${maxChange})\n")
f.write(f"Greatest Decrease in Profits: (${minChange})\n")
print(f"\nFinancial Analysis\n----------------------------------\n")
print(f"Your profit is: ${profit}")
print(f"Total Months {numMonths}")
print(f"Average Change: ${avgChance}")
print(f"Greatest Increase in Profits: (${maxChange})")
print(f"Greatest Decrease in Profits: (${minChange})")
#Cerramos documento
f.close()
Actual Results:
Financial Analysis
----------------------------------
Your profit is: $38382578
Total Months 86
Average Change: $-2315.12
Greatest Increase in Profits: ($1926159)
Greatest Decrease in Profits: ($-2196167)```
答案 0 :(得分:0)
请尝试以下操作以获取最大更改月份:
maxChangeMonth = dateProfitLossesList[changeList.index(max(changeList))]
将min替换为max函数即可。