Shivam
Shivam

Reputation: 243

How to fill column based on value of other column in dataframe?

I am trying to fill the column based on some condition. Can you please help me how to do this?

Example:

df:

   Name  Age
0   Tom   20
1  nick   21
2  nick   19
3  jack   18
4  shiv   21
5  shiv   22
6  jim    23

I have created the dataframe with one more column: df['New'] = df['Name'].shift()

   Name  Age  New
0   Tom   20  NaN
1  nick   21  Tom
2  nick   19  nick
3  jack   18  nick
4  shiv   21  jack
5  shiv   22  shiv
6  jim    23  shiv

Expected Output:

   Name  Age  New  order
0   Tom   20  NaN   1
1  nick   21  Tom   2
2  nick   19  nick  2
3  jack   18  nick  3
4  shiv   21  jack  4
5  shiv   22  shiv  4
6  jim    23  shiv  5

condition : if Name is matching the New column then check the previous row number and fill the number same number else fill the next number. It is quiet similar like dense_rank() but I don't want to use dense_rank concept here. So is there any way to fill this column?

Upvotes: 1

Views: 103

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195418

Using .cumsum() over boolean Series:

df['order'] = (df['Name'] != df['Name'].shift()).cumsum()

print(df)

Prints:

   Name  Age  order
0   Tom   20      1
1  nick   21      2
2  nick   19      2
3  jack   18      3
4  shiv   21      4
5  shiv   22      4
6   jim   23      5

Upvotes: 1

Related Questions