变换计数连续整数
transform Count consecutive integers
我有一个像
这样的数据集
id
1
2
3
4
7
8
我希望输出为:
id count
1 4
2 4
3 4
4 4
7 2
8 2
提前致谢
通过 Series.diff
, compare for not equal 1
by Series.ne
and add cumulative sum by Series.cumsum
:
为连续整数创建 Series
s = df['id'].diff().ne(1).cumsum()
然后使用Series.map
with Series.value_counts
:
df['count'] = s.map(s.value_counts())
或GroupBy.transform
with GroupBy.size
:
df['count'] = s.groupby(s).transform('size')
print (df)
id count
0 1 4
1 2 4
2 3 4
3 4 4
4 7 2
5 8 2
我有一个像
这样的数据集id
1
2
3
4
7
8
我希望输出为:
id count
1 4
2 4
3 4
4 4
7 2
8 2
提前致谢
通过 Series.diff
, compare for not equal 1
by Series.ne
and add cumulative sum by Series.cumsum
:
Series
s = df['id'].diff().ne(1).cumsum()
然后使用Series.map
with Series.value_counts
:
df['count'] = s.map(s.value_counts())
或GroupBy.transform
with GroupBy.size
:
df['count'] = s.groupby(s).transform('size')
print (df)
id count
0 1 4
1 2 4
2 3 4
3 4 4
4 7 2
5 8 2