2

Here is my input dataframe:

type
a   
a   
a   
a   
a   
b   
b   
a   
a   
a

This is my expected output:

type,   id
a   ,   1
a   ,   2
a   ,   3
a   ,   4
a   ,   5
b   ,   5
b   ,   5
a   ,   6
a   ,   7
a   ,   8

I need to generate ID column based on 'type' column. I have two types 'a' & 'b'.. as long as it is 'a' I want to increment ID. If 'b', keep previous 'a' ID. How can I do this in a Pandas dataframe?

jpp
  • 159,742
  • 34
  • 281
  • 339
JackJack
  • 75
  • 6

3 Answers3

6

You can count the cumulative sum of a Boolean series indicating when your series equals a value:

df['id'] = df['type'].eq('a').cumsum()
jpp
  • 159,742
  • 34
  • 281
  • 339
3

I tried this way, Obviously @jpp answer is coolest one. But I approached like this just to give an idea.

df=pd.DataFrame({'col1':['a','a','a','a','a','b','b','a','a','a']})
df['type']= df.groupby('col1').cumcount()+1
df.loc[df['col1']=='b','type']=np.NaN
df['type']=df['type'].ffill()
print df

O/P

  col1  type
0    a   1.0
1    a   2.0
2    a   3.0
3    a   4.0
4    a   5.0
5    b   5.0
6    b   5.0
7    a   6.0
8    a   7.0
9    a   8.0
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
0

If your DataFrame is df:

df[df=='a'].expanding().count()
r_hudson
  • 193
  • 8