Sam
Sam

Reputation: 1246

How can I rank group of rows dataframe based on index?

I have a dataframe that looks like:

df=

    a   b   c   d   e
0   1   1   0   1   1
1   0   0   0   1   1
2   0   0   0   0   0
3   1   1   1   0   0
4   0   0   0   0   0
5   1   1   1   1   1

I want to create another column "rank" which divides this dataframe every 4 row.

The result I am looking for it

df=

    a   b   c   d   e result
0   1   1   0   1   1 0
1   0   0   0   1   1 0
2   0   0   0   0   0 0
3   1   1   1   0   0 0
4   0   0   0   0   0 1
5   1   1   1   1   1 1
.   .   .   .   .   . .
.   .   .   .   .   . .

The way I am doing is:

I created a list and splited that list into group of 6

seq=[i for i in range(0,len(df))]
nn=[seq[i:i+4] for i in range(0,len(seq),4)]

then I created a function that maps the value

def map(number):
    for i in range(0,len(df)):
        if number in nn[i]:
            return i

and used map.

df['rank']=df['index'].map(lambda x: map(x))

Is there a better way to do what I am trying to do? I am sure there is more efficient way to do it? I would appreciate if someone could help me with this issue.

Thanks.

Upvotes: 0

Views: 40

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

How about:

df['rank'] = df.reset_index().index // 4

Edit to reflect Pault's comment: If you are certain that your original index is consecutive integers starting from 0, you can do without reset_index(): df.index//4.

Upvotes: 3

Erfan
Erfan

Reputation: 42886

Use np.repeat:

result = np.repeat(range(4), 4)[:len(df)]
df['result'] = result

print(df)
   a  b  c  d  e  result
0  1  1  0  1  1       0
1  0  0  0  1  1       0
2  0  0  0  0  0       0
3  1  1  1  0  0       0
4  0  0  0  0  0       1
5  1  1  1  1  1       1

Upvotes: 0

Related Questions