简体   繁体   English

Python Pandas Dataframe:如何同时将 append 索引到多个列表?

[英]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.我认为问题是,不能一次将 append 多个项目添加到列表中。

Imagine using df.iloc[3:6], But instead it is: myList.append(Start:Finish)想象一下使用 df.iloc[3:6],但它是: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) myList.append(x)、myList.append(x+1)、myList.append(x_2) (等)

I want to be able to write the same as我希望能够写出与

myList.append(THIS POSITION -> numbers inbetween AND -> FINAL POSITION) myList.append(此 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: 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)如果我正确理解您的条件n连续行,其中值正在增加(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中附加多个时间序列相关性列表? - how to append more than one list of time series correlation in python? 比列表追加方法更有效地组合Python Pandas Dataframe - Combine Python Pandas Dataframe more Efficiently than List Append Method 有效地迭代pandas.DataFrame,同时一次访问多个索引行 - Iterate pandas.DataFrame efficiently while accessing more than one index row at a time 同时使用多个索引时超出列表索引 - List index out of range when using more than one at the same time 如何在Python中一次附加一个项目? - How can i append an item more than once at the same time in Python? 我想要一个列表来附加多个参数。 Python - I want a list to append more than one arguments. python Python - Pandas - 在 Dataframe 中插入多个值到一列 - Python - Pandas - in Dataframe insert more than one value to a column Python3 在具有多个相同名称的列表中查找名称/元素的位置/索引 - Python3 find position/index of a name/element in a list with more than one of the same name 尝试在同一索引处添加多个列表的元素 - Trying to add elements at the same index for more than one list Python Pandas:如何过滤存储在不同变量中的多个表达式的数据帧? - Python Pandas: How to filter a dataframe with more than one expression stored in different variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM