python - 对某些数字之间的索引行求和 - Pandas Python

标签 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

我想计算标记==20 之间的 21、22 和 23 的发生率。唯一有效的标记位于 20 个代码之间,因此前 21 个代码无效。多个有效标记可以出现在 bookended 对 20s 中,因此我需要在一对 20s 之间出现 21,22 和 23s 的计数。

因此,在上面的示例中,只有索引 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'

如何只获取 20 对之间的值/IndexRange 中的索引之间的值?

所需的输出将是: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

关于python - 对某些数字之间的索引行求和 - Pandas Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48893307/

相关文章:

Caffe的Python接口(interface) : Error in "import caffe"

python - csv的pandas DataFrame输出端

python - 将年和月列与 Pandas 干净地组合到单个日期列

python - 如何在数据框中查找字符串并创建一个新字符串?

python - 选择按值加权的行索引

python - 为什么 'r' 用于 Python 中的正则表达式?

python - 无法运行 Spyder,因为没有名为 'PySide' 的模块

python - 使用 GAE Python GCS Client Library 访问时如何在 GCS 中分页?

具有超时和大输出 (>64K) 的 python 子进程

python - 如何在单个数据框中合并具有相同索引的行?