首页 > 解决方案 > 为什么只有 print 函数返回列表?

问题描述

所以我正在尝试做一个资金流量指数指标,到目前为止代码是:

import pandas as pd
import numpy as np
import pandas_datareader as web
import datetime as dt

#get stock prices
start = dt.datetime(2019, 12, 1)
end = dt.datetime(2020, 1, 1)
d = web.DataReader('AMD', 'yahoo', start, end)
pd.set_option('display.max_rows', 1000)

def Money_Flow_index(period = 5):
    d['typical_price'] = (d['High'] + d['Close'] + d['Low'])/3
    for i in reversed(range(len(d['typical_price']))):
        if i > 5:
            typical_price_dates = d['typical_price'][i-period:i]
            print(typical_price_dates)
Money_Flow_index()

我对 python 比较陌生,所以这可能是我不了解一些基本的东西。虽然现在代码正试图索引每个增量数据集 5 个时期的调整收盘价。因此,当我按原样运行代码时,输​​出是:

Date
2019-12-23    45.160000
2019-12-24    46.306667
2019-12-26    46.533334
2019-12-27    46.346667
2019-12-30    45.449999
Name: typical_price, dtype: float64
Date
2019-12-20    43.889999
2019-12-23    45.160000
2019-12-24    46.306667
2019-12-26    46.533334
2019-12-27    46.346667
Name: typical_price, dtype: float64
Date
2019-12-19    42.923333
2019-12-20    43.889999
2019-12-23    45.160000
2019-12-24    46.306667
2019-12-26    46.533334
Name: typical_price, dtype: float64
Date
2019-12-18    42.533334
2019-12-19    42.923333
2019-12-20    43.889999
2019-12-23    45.160000
2019-12-24    46.306667
Name: typical_price, dtype: float64
Date
2019-12-17    42.676668
2019-12-18    42.533334
2019-12-19    42.923333
2019-12-20    43.889999
2019-12-23    45.160000
Name: typical_price, dtype: float64
Date
2019-12-16    42.353333
2019-12-17    42.676668
2019-12-18    42.533334
2019-12-19    42.923333
2019-12-20    43.889999
Name: typical_price, dtype: float64
Date
2019-12-13    41.720001
2019-12-16    42.353333
2019-12-17    42.676668
2019-12-18    42.533334
2019-12-19    42.923333
Name: typical_price, dtype: float64
Date
2019-12-12    41.486666
2019-12-13    41.720001
2019-12-16    42.353333
2019-12-17    42.676668
2019-12-18    42.533334
Name: typical_price, dtype: float64
Date
2019-12-11    39.373334
2019-12-12    41.486666
2019-12-13    41.720001
2019-12-16    42.353333
2019-12-17    42.676668
Name: typical_price, dtype: float64
Date
2019-12-10    39.329999
2019-12-11    39.373334
2019-12-12    41.486666
2019-12-13    41.720001
2019-12-16    42.353333
Name: typical_price, dtype: float64
Date
2019-12-09    39.226667
2019-12-10    39.329999
2019-12-11    39.373334
2019-12-12    41.486666
2019-12-13    41.720001
Name: typical_price, dtype: float64
Date
2019-12-06    39.793334
2019-12-09    39.226667
2019-12-10    39.329999
2019-12-11    39.373334
2019-12-12    41.486666
Name: typical_price, dtype: float64
Date
2019-12-05    39.796666
2019-12-06    39.793334
2019-12-09    39.226667
2019-12-10    39.329999
2019-12-11    39.373334
Name: typical_price, dtype: float64
Date
2019-12-04    39.546666
2019-12-05    39.796666
2019-12-06    39.793334
2019-12-09    39.226667
2019-12-10    39.329999
Name: typical_price, dtype: float64
Date
2019-12-03    38.326668
2019-12-04    39.546666
2019-12-05    39.796666
2019-12-06    39.793334
2019-12-09    39.226667
Name: typical_price, dtype: float64

这是我正在寻找的,但是当我尝试return(typical_price_dates)它时只打印第一组:

Date
2019-12-23    45.160000
2019-12-24    46.306667
2019-12-26    46.533334
2019-12-27    46.346667
2019-12-30    45.449999
Name: typical_price, dtype: float64  

这个问题是当我尝试访问typical_price_dates时,它只显示第一个系列而不是所有系列。例如,如果我想做类似的事情:

typical_price = typical_price_dates
typical_volume = d['Volume']
d['money_flow_index'] = typical_price * typical_volume

它只访问第一个系列。所以基本上我怎样才能使变量typical_price_dates 等于所有系列而不仅仅是一个。谢谢你。

标签: pythonpandasindexingreturnprintf

解决方案


当您在代码中使用 return 时,循环仅运行 一次并返回结果,然后您退出函数并且此后没有循环运行,您应该做的是将典型的价格日期每次都附加python list之类的东西上,比如这

z=[]

import pandas as pd
import numpy as np
import pandas_datareader as web
import datetime as dt

#get stock prices
start = dt.datetime(2019, 12, 1)
end = dt.datetime(2020, 1, 1)
d = web.DataReader('AMD', 'yahoo', start, end)
pd.set_option('display.max_rows', 1000)

def Money_Flow_index(period = 5):
    d['typical_price'] = (d['High'] + d['Close'] + d['Low'])/3
    for i in reversed(range(len(d['typical_price']))):
        if i > 5:
            typical_price_dates = d['typical_price'][i-period:i]
            z.append(typical_price_dates)
Money_Flow_index()

for i in z:
    print(i)

推荐阅读