简体   繁体   中英

Slicing and iterating over a dataframe in Python

I have a DataFrame that looks like this:

    Date    Sell    Buy
    43016.49372 52.04   52.76
    43016.4936  52.04   52.76
    43016.49343 52.01   52.73
    43016.49339 52.02   52.74
    43016.49288 52.01   52.73
    43016.49278 52.02   52.74
    43016.4923  52.01   52.73
    43016.49217 52.06   52.78
    43016.49194 52.06   52.78
    43016.4917  52.06   52.78
    43016.49162 52.06   52.78

I would like to create a DataFrame for each row by taking the last 10 rows that come before. Then I would like to perform some calculations such as Buy-Sell.

I have tried the following code:

for row in original_df.iterrows():
    a = row
    b = row + 10
    slicing_test = original_df.iloc[a:b,]
    print(slicing_test)

And also this one:

for row in original_df.iterrows():
     slicing_test = original_df.iloc[row:row+1,] 

And it doesn't work.

您可以创建一个空数据集,然后尝试迭代地将其追加到下一行或前10行(请注意边界)。

row is a tuple in your for-loop, you can access its first element, the row-index:

for row in df.iterrows():
 a=row[0]
 b=a+10

 slicing_test = df.iloc[a:b,]
 print (slicing_test)

If you want to explicitly have your window in hand or want to perform some complicated calculations, then you might do this:

offset = 10
for ind,_ in df.iterrows():
    print(df.iloc[ind:ind+offset,:])

But if the calculation you're talking about is some mainstream calculations such as sum() or mean() or it has been defined in numpy library, then Best way to do that is to use pandas.DataFrame.rolling . As it states:

Returns: a Window or Rolling sub-classed for the particular operation

So you can use .apply afterwards

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