简体   繁体   中英

Python loop over dataframe

I have a dataframe consisting of ticker prices. It is 1 row per TICKER, DATE, PRICE, for a year. So, 365 rows per ticker. The structure is somewhat like this:

    TICKER  PRICE_DATE  OPEN_PRICE
0      IBM    20201113      115.19
1      IBM    20201112      115.63
2      IBM    20201111      118.12
3      IBM    20201110      116.69
4      IBM    20201109      117.88
.
.
.
#      AAPL   20201113      115.19
#      AAPL   20201112      115.63
#      AAPL   20201111      118.12
#      AAPL   20201110      116.69
#      AAPL   20201109      117.88

What I need to do is: for each ticker, loop through the first 3 days, and compare the OPEN price with the previous day. Basically something like: IF day1 < day2 < day3 THEN.........

So, I need 3 rows of IBM, next iteration I need 3 rows of AAPL, next iteration.........

Any easy way to do this? Or, maybe I am supposed to convert this to a different format, so I can access AAPL, price element 1, price element 2, price element 3, etc?

data.csv:

TICKER  PRICE_DATE  OPEN_PRICE
IBM    20201113      115.19
IBM    20201112      115.63
IBM    20201111      118.12
IBM    20201110      116.69
IBM    20201109      117.88
AAPL   20201113      115.19
AAPL   20201112      115.63
AAPL   20201111      118.12
AAPL   20201110      116.69
AAPL   20201109      117.88
INTC   20201113      115.19
INTC   20201112      115.63
INTC   20201111      118.12
INTC   20201110      116.69
INTC   20201109      117.88

Code:

import pandas as pd
import numpy as np

df = pd.read_csv('data.csv', delimiter='\s+')
print(df, '\n')

arr_t = np.dtype([('TICKER','S4')]) 
arr_t = df['TICKER'].unique()

df = df.set_index('TICKER')
print(df)
rows_per_ticker = df.loc[arr_t[0]].PRICE_DATE.count()

r = 0
while r < rows_per_ticker:
    for i in range(0,len(arr_t)):
        print(df.loc[arr_t[i]].iloc[r:r+3, :])
    r+=3

Output:

   TICKER  PRICE_DATE  OPEN_PRICE
0     IBM    20201113      115.19
1     IBM    20201112      115.63
2     IBM    20201111      118.12
3     IBM    20201110      116.69
4     IBM    20201109      117.88
5    AAPL    20201113      115.19
6    AAPL    20201112      115.63
7    AAPL    20201111      118.12
8    AAPL    20201110      116.69
9    AAPL    20201109      117.88
10   INTC    20201113      115.19
11   INTC    20201112      115.63
12   INTC    20201111      118.12
13   INTC    20201110      116.69
14   INTC    20201109      117.88

        PRICE_DATE  OPEN_PRICE
TICKER
IBM       20201113      115.19
IBM       20201112      115.63
IBM       20201111      118.12
IBM       20201110      116.69
IBM       20201109      117.88
AAPL      20201113      115.19
AAPL      20201112      115.63
AAPL      20201111      118.12
AAPL      20201110      116.69
AAPL      20201109      117.88
INTC      20201113      115.19
INTC      20201112      115.63
INTC      20201111      118.12
INTC      20201110      116.69
INTC      20201109      117.88
        PRICE_DATE  OPEN_PRICE
TICKER
IBM       20201113      115.19
IBM       20201112      115.63
IBM       20201111      118.12
        PRICE_DATE  OPEN_PRICE
TICKER
AAPL      20201113      115.19
AAPL      20201112      115.63
AAPL      20201111      118.12
        PRICE_DATE  OPEN_PRICE
TICKER
INTC      20201113      115.19
INTC      20201112      115.63
INTC      20201111      118.12
        PRICE_DATE  OPEN_PRICE
TICKER
IBM       20201110      116.69
IBM       20201109      117.88
        PRICE_DATE  OPEN_PRICE
TICKER
AAPL      20201110      116.69
AAPL      20201109      117.88
        PRICE_DATE  OPEN_PRICE
TICKER
INTC      20201110      116.69
INTC      20201109      117.88

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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