是否有Python函数用于建立以2个数组为参数的平均价格

时间:2019-05-20 13:15:04

标签: python pandas numpy arraylist

首先,我绝对是Python的新手,请尝试自己学习。我进行了以下练习,并尝试使用NumPyPandas来解决它。

使用2个数组记录股票交易。交易股票的名称进入第一个数组。交易的股票价格进入第二个数组的相同位置。 例如,数组可能看起来像这样:

String[] shares = {"HSBC","o2","Vodafone","Vodafone","HSBC","o2","Vodafone"};
double[] prices = {6.40,9.99,2.40,2.45,6.37,10.10,2.50};

我需要编写一个名为averagePrices的方法,该方法将2个数组作为参数。该方法应打印出每个公司的平均股价,四舍五入到小数点后两位。上面的数组将产生:

Vodafone: £2.45
HSBC: £6.39
o2: £10.05

仅使用ListsFor循环就可以得到正确的结果,但是我想处理此问题,就好像列表会更长且不够方便,无法键入单个值步骤的所有位置靠我自己一步一步。 因此,我正在寻找Excel中的VLookup之类的函数或使用pandas的相似函数,该函数可以汇总不同份额的相应价格以建立平均值。

import numpy as np

shares = np.array(["HSBC", "o2", "Vodafone", "Vodafone", "HSBC", "o2", "Vodafone"])
prices = np.array([6.40, 9.99, 2.40, 2.45, 6.37, 10.10, 2.50])

HSBC_bool = shares == "HSBC"
o2_bool = shares == "o2"
Vodafone_bool = shares == "Vodafone"

HSBC = shares[HSBC_bool]
o2 = shares[o2_bool]
Vodafone = shares[Vodafone_bool]

HSBC_count = HSBC.shape
o2_count = o2.shape
Vodafone_count = Vodafone.shape

HSBC_share = []
o2_share = []
Vodafone_share = []

if HSBC_bool.any == True:
    HSBC_share.append(prices[:])

if o2_bool.any == True:
    o2_share.append(prices[:])

if Vodafone_bool.any == True:
    Vodafone_share.append(prices[:])

print(Vodafone_share)
print(o2_share)
print(HSBC_share)


avg_HSBC = sum(HSBC_share) / HSBC_count
avg_o2 = sum(o2_share) / o2_count
avg_Vodafone = sum(Vodafone_share) / Vodafone_count

print(round(avg_HSBC, 2))
print(round(avg_o2, 2))
print(round(avg_Vodafone, 2))

3 个答案:

答案 0 :(得分:3)

尝试使用pandasgroupby().mean()函数。我建议您先在线阅读并玩一会儿,然后再提出要求,否则您将学不到很多东西。

链接:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.mean.html

在您的情况下:

shares = np.array(["HSBC", "o2", "Vodafone", "Vodafone", "HSBC", "o2", "Vodafone"])
prices = np.array([6.40, 9.99, 2.40, 2.45, 6.37, 10.10, 2.50])

df = pd.DataFrame({'shares': shares,
                   'prices': prices})

df.groupby('shares').mean()

这将导致:

          prices
shares
HSBC       6.385
Vodafone   2.450
o2        10.045

答案 1 :(得分:1)

您可以使用np.unique函数迭代不同的名称。然后使用np.argwhere计算相应的平均值。

这是一个可行的示例:

shares = np.array(["HSBC", "o2", "Vodafone", "Vodafone", "HSBC", "o2", "Vodafone"])
prices = np.array([6.40, 9.99, 2.40, 2.45, 6.37, 10.10, 2.50])

def Average_price(shares, prices):
    average_price={}
    for share in np.unique(shares):
        value = 0
        for indice in np.argwhere(shares == share):
            value += prices[indice]
        average_price[share] = value / len(np.argwhere(shares == "HSBC"))
    return average_price

average_price = Average_price(shares, prices)
In [1] : print(average_price)
Out[1] : {'HSBC': array([ 6.385]), 'Vodafone': array([ 2.450]), 'o2': array([10.045])}

答案 2 :(得分:0)

一种棘手的方法是计算数量,然后将价格除以该数量,然后计算总和并创建最终字典:)

不需要软件包


shares = ["HSBC", "o2", "Vodafone", "Vodafone", "HSBC", "o2", "Vodafone"]
prices = [6.40, 9.99, 2.40, 2.45, 6.37, 10.10, 2.50]

# Get unique shares 
unique_shares = list(set(shares))
# Get count of shares
count_shares = [shares.count(x) for x in shares]
# Get average price with corresponding share
avg_shares = [round(x/y, 2) for x, y in zip(prices, count_shares)]
# Zip result into single list of tuple
avg_prices_per_share = list(zip(shares, avg_shares))
# Get average based on count of shares and computed averaprice for each share
result  = {unique_key: round(sum(v for k, v in avg_prices_per_share if k==unique_key), 2)
    for unique_key in unique_shares}

输出:

{'HSBC': 6.39, 'Vodafone': 2.45, 'o2': 10.05}