2

I have col1 in a pandas df. I want to make col2:

col1    col2
1       1
1       2
1       3
1       4
2       2
2       3
2       4
3       3
3       4
4       4

In other words, for each distinct value in col1 I want col2 to be increasing integers that start with the value in col1 and counts up by one until there are no more rows.

The data is structured in a way so that max(col1) = 2450:

  • when col1 = 1, there are 2450 rows
  • when col1 = 2, there are 2449 rows
  • when col1 = 2450, there is 1 row

1 Answer 1

4

You could GroupBy column col1, take the cumcount of the groups and add col1:

df['col2'] = df.groupby('col1').cumcount().add(df.col1)

Output

    col1  col2
0     1     1
1     1     2
2     1     3
3     1     4
4     2     2
5     2     3
6     2     4
7     3     3
8     3     4
9     4     4
Sign up to request clarification or add additional context in comments.

1 Comment

Nailed it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.