Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对索引位于特定数字之间的行进行求和-0_Python_Pandas - Fatal编程技术网

Python 对索引位于特定数字之间的行进行求和-0

Python 对索引位于特定数字之间的行进行求和-0,python,pandas,Python,Pandas,我有一个以下格式的csv Time Marker 0 2104 21 1 2109 20 2 2485 21 3 2491 20 4 2867 22 5 2997 2 6 3248 23 我想计算21,22和23秒的发生率,在标记==20之间。唯一有效的标记在20个代码之间,因此前21个无效。多个有效标记可以出现在一对20之间,因此我需要一个21、

我有一个以下格式的csv

       Time     Marker
0       2104    21
1       2109    20
2       2485    21
3       2491    20
4       2867    22
5       2997    2
6       3248    23
我想计算21,22和23秒的发生率,在标记==20之间。唯一有效的标记在20个代码之间,因此前21个无效。多个有效标记可以出现在一对20之间,因此我需要一个21、22和23的计数,它们出现在一对20之间

因此,在上面的示例中,只有索引2可能是有效代码,因为它位于两个20之间

我有一个满足Marker==20条件的索引列表

Indexrange = df.index[df['Marker'] == 20].tolist()
[1,
 3,
 10,
 19,
 22,
 25,
 29,
 32,]
我如何循环浏览索引列表并计算每对20秒中每个21、22、23的发生率

到目前为止,我已经:

TwentyOnes=0
TwentyTwos=0
TwentyThrees=0

for i in Indexrange:
    for index, row in df.iterrows():
        if index.between(i, i+1):
            if Marker == 21
                Count_of_21s +=
            if Marker == 22
                Count_of_22s +=
            if Marker == 23
                Count_of_23s +=
            else:
                InvalidCount+=
但是我越来越

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-4a72c2a77924> in <module>()
  5 for i in Indexrange:
  6     for index, row in df.iterrows():
----> 7         if index.between(i,i+1):
  8             print(index, row['Marker'])

AttributeError: 'int' object has no attribute 'between'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
5对于索引范围内的i:
6对于索引,df.iterrows()中的行:
---->7如果指数介于(i,i+1)之间:
8打印(索引,第[‘标记’]行)
AttributeError:'int'对象没有属性'between'
如何仅获取IndexRange中的20对之间/索引之间的值

所需的输出是:Counts_of_21s=int、Counts_of_22s=int、Counts_of_23s=int、InvalidCount=int,这似乎是您需要的

df.groupby(df.Marker.eq(20).cumsum()).Marker.value_counts()
Out[1013]: 
Marker  Marker
0       21        1
1       20        1
        21        1
2       2         1
        20        1
        22        1
        23        1
Name: Marker, dtype: int64
更新

df=df.assign(yourid=df.Marker.eq(20).cumsum())
df.loc[(df.yourid<df.yourid.max())&(df.yourid>df.yourid.min())&(df.Marker!=20),:].groupby('yourid').Marker.value_counts()
Out[1021]: 
yourid  Marker
1       21        1
Name: Marker, dtype: int64
df=df.assign(yourid=df.Marker.eq(20.cumsum())
df.loc[(df.youriddf.yourid.min())和(df.Marker!=20),:].groupby('yourid').Marker.value_counts()
Out[1021]:
yourid标记
1       21        1
名称:Marker,数据类型:int64
这是我的解决方案:

import pandas as pd

csv_df = pd.read_csv('between.txt')

markers = csv_df['Marker'].tolist()
indexrange = csv_df.index[csv_df['Marker'] == 20].tolist()
list_dicts = []

for x in range(len(indexrange)-1):
    currentgroup = {'21': markers[indexrange[x]:indexrange[x+1]].count(21),
                    '22': markers[indexrange[x]:indexrange[x+1]].count(22),
                    '23': markers[indexrange[x]:indexrange[x+1]].count(23)
                    }
    list_dicts.append(currentgroup)

i = 1
for list in list_dicts:
    print(f'Grouping {i}', list)
    i = i+1

温的要好得多。

很接近,但它并没有解决最初的问题。在给定的示例中,唯一有效的代码是索引2,因为它位于一对20之间。这确实是一个很好的解决方案。比下面我的要好得多,因为它将获得永远的值,而不仅仅是我的硬编码值。