python - 连续相同值的总和

标签 python python-3.x pandas cumsum

如何获得 pandas 系列中连续 1 的总和?

例如,s = pd.Series([5, 1, 4, 1, 1, 2, 3, 1, 1, 1, 4])。我想获得 pd.Series([0, 1, 0, 1, 2, 0, 0, 1, 2, 3, 0])

( Pandas 0.18.0)

最佳答案

你可以试试groupbycumcounts1 != 1cumsum 进行比较:

print s1.groupby((s1 != 1).cumsum()).cumcount()
0     0
1     1
2     0
3     1
4     2
5     0
6     0
7     1
8     2
9     3
10    0
dtype: int64

更好的解释:

df = pd.DataFrame(s1, columns=['orig'])
df['not1'] = s1 != 1
df['cumsum'] = (s1 != 1).cumsum()
df['cumcount'] = s1.groupby((s1 != 1).cumsum()).cumcount()
#s1.groupby((s1 != 1).cumsum()).cumcount() is same as:
df['cumcount1'] = df.groupby('cumsum')['orig'].cumcount()
print df
    orig   not1  cumsum  cumcount  cumcount1
0      5   True       1         0          0
1      1  False       1         1          1
2      3   True       2         0          0
3      4   True       3         0          0
4      1  False       3         1          1
5      1  False       3         2          2
6      2   True       4         0          0
7      3   True       5         0          0
8      1  False       5         1          1
9      1  False       5         2          2
10     1  False       5         3          3
11     4   True       6         0          0

或者:

print (s1 == 1) * (s1.groupby((s1 != s1.shift()).cumsum()).cumcount() + 1)
0     0
1     1
2     0
3     1
4     2
5     0
6     0
7     1
8     2
9     3
10    0
dtype: int64

解释:

df = pd.DataFrame(s1, columns=['orig'])
df['compare_shift'] = s1 != s1.shift()
df['cumsum'] = (s1 != s1.shift()).cumsum()
df['cumcount'] = s1.groupby((s1 != s1.shift()).cumsum()).cumcount() + 1
df['cumcount1'] = df.groupby('cumsum')['orig'].cumcount() + 1
df['is1'] = (s1 == 1)
#True in converted to 1, False to 0
df['fin'] = (s1 == 1) * (s1.groupby((s1 != s1.shift()).cumsum()).cumcount() + 1)
print df
    orig compare_shift  cumsum  cumcount  cumcount1    is1  fin
0      5          True       1         1          1  False    0
1      1          True       2         1          1   True    1
2      3          True       3         1          1  False    0
3      4          True       4         1          1  False    0
4      1          True       5         1          1   True    1
5      1         False       5         2          2   True    2
6      2          True       6         1          1  False    0
7      3          True       7         1          1  False    0
8      1          True       8         1          1   True    1
9      1         False       8         2          2   True    2
10     1         False       8         3          3   True    3
11     4          True       9         1          1  False    0

关于python - 连续相同值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36231020/

相关文章:

python - 良好的 python XML 解析器,可处理命名空间繁重的文档

python - 在 pyrango arangodb 中创建唯一索引

python - 获取 IndexError : string index out of range in python

python - Python Pandas 中的困难 Dataframe Reshape

python - 将单位与 Pandas DataFrame 关联

python - 将所有文件附加到 Outlook 电子邮件

python - 如何使用诗歌和 pyproject.toml 保持 python 包的开发环境和生产环境之间的依赖关系一致

python - 在 macOS Mojave 上安装 Pillow - 运行 python 3.7

python - 获取groupby后每列的百分比

python - 将 python 列表转换为数据帧时,ValueError : 4 columns passed, 传递的数据有 3 列。如果3通过了如何添加空白值?