简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM