如何将几个函数应用于列表中的多个代码? (代码改进)

How can I apply a couple of functions to multiple tickers in a list? (Code improvement)

所以我目前正在学习如何使用 numpy、pandas 等来分析 python 中的财务数据...我从一个小脚本开始,希望它能对一些人进行排名通过两个选定日期之间的价格变化来选择股票。 我的第一个脚本是:

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

#Edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

#Getting the data:    
AAPL = web.DataReader('AAPL', 'google', start, end)
GOOGL = web.DataReader('GOOGL', 'google', start, end)
YHOO = web.DataReader('YHOO', 'google', start, end)
MSFT = web.DataReader('MSFT', 'google', start, end)
AMZN = web.DataReader('AMZN', 'google', start, end)
DAI = web.DataReader('DAI', 'google', start, end)

#Calculating the change:
AAPLkey = (AAPL.ix[start]['Close'])/(AAPL.ix[end]['Close'])
GOOGLkey = (GOOGL.ix[start]['Close'])/(GOOGL.ix[end]['Close'])
YHOOkey = (YHOO.ix[start]['Close'])/(YHOO.ix[end]['Close'])
MSFTkey = (MSFT.ix[start]['Close'])/(MSFT.ix[end]['Close'])
AMZNkey = (AMZN.ix[start]['Close'])/(AMZN.ix[end]['Close'])
DAIkey = (DAI.ix[start]['Close'])/(DAI.ix[end]['Close'])

#Formatting the output in a sorted order:
dict1 = {"AAPL" : AAPLkey, "GOOGL" : GOOGLkey, "YHOO" : YHOOkey, "MSFT" : MSFTkey, "AMZN" : AMZNkey, "DAI" : DAIkey}

out = sorted(dict1.items(), key=itemgetter(1), reverse = True)

for tick , change in out:
    print (tick,"\t", change)

我现在显然想让这段时间更短,这是我目前所做的:

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

#Edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

for eq in stocks:
    eq = web.DataReader(eq, 'google', start, end)

for legend in eq:
    legend = (eq.ix[start]['Close'])/(eq.ix[end]['Close'])

print (legend)

计算有效,但问题是这只输出列表中项目的最后一个值 (DAI)。 那么接下来要做什么才能获得与我的第一个代码相同的结果?

您可以将 print 语句移动到循环中。

喜欢:

for legend in eq:
    legend = (eq.loc[start]['Close'])/(eq.loc[end]['Close'])
    print(legend)

改进的答案: 从上一个循环中删除标签循环和打印值:

for eq in stocks:
    df = web.DataReader(eq, 'google', start, end)
    print((df.loc[start]['Close'])/(df.loc[end]['Close']))

当您在第 for eq in stocks 行循环遍历股票时,您正在将结果保存到 eq。所以在每次迭代中它都会被覆盖。您应该将结果存储在列表中,就像我使用 data 所做的那样。

然后遍历包含数据帧的 data 列表,然后使用适当的选择。

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

# edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

# store all the dataframes in a list
data = []

for eq in stocks:
    data.append(web.DataReader(eq, 'google', start, end))

# print required fields from each dataframe
for df in data:
    print (df.ix[start]['Close'])/(df.ix[end]['Close'])

输出:

0.624067042032
0.612014075932
0.613225417599
0.572179539021
0.340850298595
1.28323537643

多亏了其他的回答,他们都帮了大忙。多亏了帮助,这是我最终改进的脚本:

import numpy as np
import pandas as pd
from pandas_datareader import data as web
from pandas import Series, DataFrame
import datetime
from operator import itemgetter

# edit below for 2 dates you wish to calculate:
start = datetime.datetime(2014, 7, 15)
end = datetime.datetime(2017, 7, 25)

stocks = ('AAPL', 'GOOGL', 'YHOO', 'MSFT', 'AMZN', 'DAI')

dict1 = {}

for eq in stocks:
    df = web.DataReader(eq, 'google', start, end)
    k = ((df.loc[start]['Close'])/(df.loc[end]['Close']))
    dict1 [eq] = k

out = sorted(dict1.items(), key=itemgetter(1), reverse = True)

for tick , change in out:
    print (tick,"\t", change)