简体   繁体   中英

Python Pandas Dataframe: How to append more than one index to a list at the same time?

Rewritten with what I think is hopefully an easier question to understand.

What I think the problem is, is not being able to append more than one item to a list at a time.

Imagine using df.iloc[3:6], But instead it is: myList.append(Start:Finish)

pseudocode

# looping through an index and adding something to it when true
for (x)each index in the index:
    if condition is true:
        myList.append(x)

below code works, but it is inefficient

# looping through an index and adding something to it when true
for (x)each index in the index:
    if condition is true:
        myList.append(x),myList.append(x+1),myList.append(x+2)

what I would like to be able to do

# looping through an index and adding something to it when true
for (x)each index in the index:
    if condition is true:
        myList.append(x -> x+2) #myList.append(x+1),myList.append(x+2)

# so I want: myList.append(x to x+1 to x+2) 
# but without having to keep writing x+this , x+that ...

Instead of this

myList.append(x), myList.append(x+1), myList.append(x_2) (etc)

I want to be able to write the same as

myList.append(THIS POSITION -> numbers inbetween AND -> FINAL POSITION)

If it is not clear enough I am sorry and I will write spaghetti code until I figure it out

IIUC:

a = []
g = lambda x,y: list(range(x,x+y))
a.append(g(7,4))
a.append(g(11,4))

a:

[[7, 8, 9, 10], [11, 12, 13, 14]]

If I understand properly your condition n consecutive rows where the value is increasing (x1<x2<x3<x4)

This solution will help:

https://stackoverflow.com/a/65090526/6660373

You need to modify as per your requirement.

Borrowing the code from that answer:

# Is the current "Close" increasing or same (compared with previous row)
df['incr'] = df.Close >= df.Close.shift(fill_value=0)
# Generate the result column
df['DaysDecr'] = df.groupby(df.incr.cumsum()).apply(
    lambda grp: (~grp.incr).cumsum()).reset_index(level=0, drop=True)
df.drop(columns='incr', inplace=True)

N=3
idx = df.loc[(df['DaysDecr'].rolling(window=N , min_periods=N)\
                          .apply(lambda x: (x==0).all()).eq(1))].index
f = lambda x: list(range(x-N+1,x+1))
for i in idx:
    print(f(i)) # <--------- here is your indices where the condition is satisfied

[0, 1, 2]

df:

    Date        Close       incr    DaysDecr
0   2015-11-27  105.449997  True    0
1   2015-11-30  106.239998  True    0
2   2015-12-01  107.120003  True    0
3   2015-12-02  106.070000  False   1
4   2015-12-03  104.379997  False   2
5   2020-11-18  271.970001  True    0
6   2020-11-19  272.940002  True    0
7   2020-11-20  269.700012  False   1
8   2020-11-23  268.429993  False   2
9   2020-11-24  276.920013  True    0

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