我有一个名为'currency.csv'的CSV文件。文件内容如下:
我需要计算5年内每种货币使用numpy的平均费率,每年每种货币的平均费率,5年期间每种货币的最低和最高汇率,每种货币的汇率的标准差5年期间的货币和每种货币每年汇率的标准差。
有人可以帮助我,我是python领域的新手,我无法完成它。
答案 0 :(得分:0)
'''
OP has a csv file with currency in it. He wants the average rate using each currency
over the past 5 years, the average rate of each currency over each year, the
highest and lowest exchange rate of each currency over the past 5 years, and
the standard deviation of each currency over the past 5 years.
'''
import numpy as np
currencies = np.array([86.43, 87.45, 90.05, 90.20, 97.05, 20.95, 23.05, 29.76, 27.54, 25.55, 40.56, 47.69, 45.98, 45.89, 43.05]).reshape(3, 5)
mean_currency1 = np.mean(currencies[0,])
print('The mean exchange rate for currency 1 over the past 5 years is: {} '.format(mean_currency1))
mean_currency2 = np.mean(currencies[1,])
print('The mean exchange rate for currency 2 over the past 5 years is: {} '.format(mean_currency2))
mean_currency3 = np.mean(currencies[2,])
print('The mean exchange rate for currency 3 over the past 5 years is: {} '.format(mean_currency3))
year_1_currency1 = currencies[0, 0]
print('The average exhcnage rate value for currency 1 in year 1 was: {}'.format(year_1_currency1))
max_currency1 = np.max(currencies[0,])
print('The maximum exchange rate value for currency 1 over the past 5 years was: {}'.format(max_currency1))
min_currency1 = np.min(currencies[0,])
print('The minimum exchange rate value for currency 1 over the past 5 years was: {}'.format(min_currency1))
stdev_currency1 = np.std(currencies[0,])
print('The standard deviation for the exchange rate for currency 1 over the past 5 years was: {}'.format(stdev_currency1))
您只需使用.csv文件中的数据,而不是创建自己的np.array。我只是创建了自己的数组,因为我没有.csv的汇率。我也没有找到每种货币的统计数据,因为我想给你一个关于你需要做什么的广泛想法。导入.csv后(如果你不知道如何导入数据我也可以帮助你),你只需要制作切片以获得你想要的统计数据。因此对于货币1,我们只是将第0行中的数据切片,我们可以得到该特定行的平均值。我们可以为您需要的每一行货币汇率做到这一点。对于1年平均值,我们可以采用每年数据的平均值(如果没有此处显示的.csv文件格式,很难确切地知道数据的格式化方式)。为了找到最小值和最大值,我们可以为每行货币汇率数据使用np.max函数和np.min函数(切片数据)。我们可以使用np.std函数来查找一行货币数据的标准差(假设您的货币汇率数据按行格式化)。最后,这是输出:
The mean exchange rate for currency 1 over the past 5 years is: 90.236
The mean exchange rate for currency 2 over the past 5 years is: 25.37
The mean exchange rate for currency 3 over the past 5 years is: 44.634
The average exhcnage rate value for currency 1 in year 1 was: 86.43
The maximum exchange rate value for currency 1 over the past 5 years was: 97.05
The minimum exchange rate value for currency 1 over the past 5 years was: 86.43
The standard deviation for the exchange rate for currency 1 over the past 5 years was: 3.7071261106145252
正如您所看到的,我没有对每种货币执行操作,但您只需要切割要使用的行(或列,具体取决于csv的格式)。我做了你要求货币1的所有事情作为例子。