简体   繁体   中英

Fetch consecutive rows from python data frame and store in list

I am trying to fetch 4 consecutive rows from a data frame and store them in a list. And would like to stop when "i" reaches its value. Method1

 dfList = [] for index, row in df.iterrows(): tempDF = df.iloc[row[i]:row[i+3]].reset_index(drop=True) dfList.append(tempDF)

error: TypeError: can only concatenate str (not "int") to str

Method2:

 dfList = [] for i in df: tempDF = df.iloc[row[i]:row[i+3]].reset_index(drop=True) dfList.append(tempDF

 dfList = [] for index, row in df.iterrows(): tempDF = df[(df.loc[(row[i], row[i+3]))]].reset_index(drop=True) dfList.append(tempDF)

Nothing is working.

original dataframe:

 import numpy as np import pandas as pd import matplotlib.pyplot as plt from datetime import date, timedelta, datetime import time from google.colab import files import warnings warnings.filterwarnings("ignore") # fix_yahoo_finance to fetch detch of GME directly from the yahoo finance website import fix_yahoo_finance as yf yf.pdr_override() # input values symbol = 'GME' # GME ticker symbol start_date = '2018-01-01' end_date = '2020-12-31' # Read data from the website df = yf.download(symbol,start_date,end_date) # View data related information print(df.tail()) print(len(df)) print(df.info()) # check for whether any null values print(df.index) #type of index df.reset_index(inplace=True) df.head() Date Open High Low Close Adj Close Volume 0 2018-01-02 17.959999 18.290001 17.780001 18.260000 15.953856 2832700 1 2018-01-03 18.290001 18.370001 17.920000 18.200001 15.901433 3789200 2 2018-01-04 18.200001 18.379999 17.959999 18.320000 16.006279 2781300 3 2018-01-05 18.379999 18.730000 18.219999 18.680000 16.320812 3019000 4 2018-01-08 18.799999 19.400000 18.799999 19.230000 16.801352 3668400

I want output like: Output

Hey I think you need to pass "index" instead of "row['index']" in your iloc parameters.

dfList = []
for index, row in df.iterrows():
    # As long as there are 3 more rows to concatenate
    if(index < df.shape[0] - 3):
        tempDF = df.iloc[index:index+4].reset_index(drop=True)
        dfList.append(tempDF)

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