Federico Gentile
Federico Gentile

Reputation: 5940

Replace column values according to values of consecutive rows in pandas

I have a dataframe df_in defined as so:

import pandas as pd
dic_in = {'A':['aa','bb','cc','dd','ee','ff','gg','uu','xx','yy','zz'],
       'B':['200','200','200','400','400','500','700','700','900','900','200'],
       'C':['da','cs','fr','fs','se','at','yu','j5','31','ds','sz']}
df_in = pd.DataFrame(dic_in) 

I want to investigate column B in such a way that all the rows having the same consecutive value are assigned a new value (according to a specific rule which i am about to describe). I will give an example to be more clear: the first three rows['B'] are equal to 200. Therefore all of them will have assigned the number 1; the fourth and fifth row['B'] are equal to 400 so they will be assigned number 2. The procedure repeats until the end. The final result (df_out) should look like this:

# BEFORE #                # AFTER #
In[121]:df_in             In[125]df_out
Out[121]:                 Out[125]: 
     A    B   C                A  B   C
0   aa  200  da           0   aa  1  da
1   bb  200  cs           1   bb  1  cs
2   cc  200  fr           2   cc  1  fr
3   dd  400  fs           3   dd  2  fs
4   ee  400  se           4   ee  2  se
5   ff  500  at           5   ff  3  at
6   gg  700  yu           6   gg  4  yu
7   uu  700  j5           7   uu  4  j5
8   xx  900  31           8   xx  5  31
9   yy  900  ds           9   yy  5  ds
10  zz  200  sz           10  zz  6  sz

Notice:

Can you suggest me a smart way to achieve such result using pandas?

PS: mapping values manually is not helpful since this is a test case, and eventually I will have thousands of rows to map. It should be something automatic.

Upvotes: 2

Views: 897

Answers (1)

jezrael
jezrael

Reputation: 862611

You can compare by ne shifted column and then use cumsum:

print (df_in.B.ne(df_in.B.shift()))
0      True
1     False
2     False
3      True
4     False
5      True
6      True
7     False
8      True
9     False
10     True
Name: B, dtype: bool

df_in.B = df_in.B.ne(df_in.B.shift()).cumsum()
#same as !=, but 'ne' is faster
#df_in.B = (df_in.B != df_in.B.shift()).cumsum()
print (df_in)
     A  B   C
0   aa  1  da
1   bb  1  cs
2   cc  1  fr
3   dd  2  fs
4   ee  2  se
5   ff  3  at
6   gg  4  yu
7   uu  4  j5
8   xx  5  31
9   yy  5  ds
10  zz  6  sz

Upvotes: 3

Related Questions