简体   繁体   English

使用pandas为库存迭代DataFrame

[英]Iterating through DataFrame using pandas for stocks

I am relatively new to python and pandas . 我对python和pandas比较pandas I have a DataFrame with a few stocks and their associated 'low' prices for the past few days. 我有一个DataFrame其中包含一些股票以及过去几天的相关“低价”。 I am trying to iterate through each stock (right now I only have 3 but will eventually have thousands) and then for each stock, I want to see if the current day's 'low' price is greater than yesterday's low price AND I want to see if yesterday's low price is less than the low price from 2 days ago. 我试图遍历每个股票(现在我只有3个,但最终将有数千个)然后对于每个股票,我想看看当前的'低'价格是否高于昨天的低价我想看到如果昨天的低价低于2天前的低价。 For each stock that meets this criteria, I eventually want to export them to a csv file. 对于符合此条件的每个库存,我最终希望将它们导出到csv文件。

list = ['IBM', 'AMZN', 'FB'] 

stockData = DataReader(list,  'yahoo', datetime(2016,06,8), datetime.today().utcnow())

low = stockData['Low']

low0 = low.iloc[-1]
low1 = low.iloc[-2]
low2 = low.iloc[-3]

The variables low0, low1, and low2 are probably not necessary but I do like how they splice out the specific data I want. 变量low0,low1和low2可能不是必需的,但我确实喜欢它们如何拼接出我想要的特定数据。

I then tried iterating over each stock in my list with my function: 然后我尝试使用我的函数遍历列表中的每个股票:

for stock in list:
    if low0 > low1 and low1 < low2:
        print True
    else: 
        print False

This is the error I get: ValueError: The truth value of a Series is ambiguous. 这是我得到的错误:ValueError:系列的真值是不明确的。 Use a.empty, a.bool(), a.item(), a.any() or a.all(). 使用a.empty,a.bool(),a.item(),a.any()或a.all()。

I would appreciate any input. 我很感激任何意见。

To identify whether the Low has been increasing for the last three days, you can use the following: 要确定过去三天Low是否一直在增加,您可以使用以下内容:

stockData = stockData.sort_index(ascending=False).iloc[:3] # reverse order, use last three days

either a condition that compares Low between adjacent days, and returns True if Low has been increasing in both cases: 要么是在相邻天之间比较Low的条件,要么在Low两个情况下一直增加时返回True

stockData[(stockData.Low < stockData.Low.shift(1)) & (stockData.Low.shift(1) < stockData.Low.shift(2))]

Or check if the difference between the last three Low prices is negative (because the most recent day now comes first) for all days: 或者检查所有日子中最后三个Low价格之间的差异是否为负(因为最近的一天现在是第一个):

stockData.Low.diff().dropna() < 0).all()

So for your application: 所以适合您的应用:

for stock in ['IBM', 'AMZN', 'FB']:
    stockData = DataReader(stock, 'yahoo', datetime(2016, 6, 8), datetime.today().utcnow()).sort_index(ascending=False).iloc[:3]
    print('\n', stockData.Low)
    print(stock, (stockData.Low.diff().dropna()<0).all())
    print(stock, stockData[(stockData.Low < stockData.Low.shift(1)) & (stockData.Low.shift(1) < stockData.Low.shift(2))].Low.any())


 Date
2016-06-15    150.600006
2016-06-14    150.399994
2016-06-13    150.279999
Name: Low, dtype: float64
IBM True
IBM True

 Date
2016-06-15    713.349976
2016-06-14    712.270020
2016-06-13    711.159973
Name: Low, dtype: float64
AMZN True
AMZN True

 Date
2016-06-15    114.070000
2016-06-14    113.580002
2016-06-13    113.309998
Name: Low, dtype: float64
FB True
FB True

This is an example of a similar, but slightly different approach to this problem. 这是针对此问题的类似但略有不同的方法的示例。 I am using dummy values to demonstrate. 我正在使用虚拟值来演示。

First, I create a dataframe. 首先,我创建一个数据帧。

dates = pd.date_range('20130101', periods=3)
IBM = [5,3,2]
AMZN = [1,7,6]
FB = [4,7,9]
df = pd.DataFrame({'IBM': IBM,'AMZN': AMZN,'FB':FB}, index=dates)
df
          AMZN  FB  IBM
2013-01-01  1   4   5
2013-01-02  7   7   3
2013-01-03  6   9   2

I use .shift() to track how much the values went up or down during the second and third day compared to the first day and the second day in the dataframe. 我使用.shift()来跟踪第二天和第三天的值与数据帧中的第一天和第二天相比上升或下降的程度。 I do this by subtracting df.shift(1) from df . 我这样做是通过从df减去df.shift(1)来实现的。 The first day value will be replaced by NaN . 第一天的价值将由NaN取代。

df - df.shift(1)
           AMZN     FB     IBM
2013-01-01  NaN     NaN     NaN  
2013-01-02  6.0     3.0     -2.0
2013-01-03  -1.0    2.0     -1.0

If you prefer True or False , you can check if the values are higher or lower than 0 . 如果您更喜欢TrueFalse ,则可以检查值是高于还是低于0 So, in this case, True will mean up and False means down and the first day, the starting value, will be replaced by False . 因此,在这种情况下, True表示向上, False表示向下,第一天,起始值将被False替换。

df - df.shift(1) > 0
            AMZN    FB      IBM
2013-01-01  False   False   False
2013-01-02  True    True    False
2013-01-03  False   True    False 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM