我正试图从数据帧中提取4个连续的行,并将它们存储在列表中。并且希望在";i〃;达到其价值。方法1
dfList = []
for index, row in df.iterrows():
tempDF = df.iloc[row[i]:row[i+3]].reset_index(drop=True)
dfList.append(tempDF)
error:TypeError:只能将str(而不是"int"(连接到str
方法2:
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)
什么都不起作用。
原始数据帧:
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
我想要这样的输出:输出
嘿,我想你需要通过"索引";而不是";行[‘索引’]";在你的iloc参数中。
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)