Home > Software engineering >  Create row count using odd numbers in Python
Create row count using odd numbers in Python

Time:06-02

There has to be a simple solution here. I know how to use cumcount but I want it to count in odd numbers. For example I have the following DF. Searches have been pretty useless as they all pull up counting the "odd" numbers.

letters = [ "A", "A", "A", "B", "B", "B"]

df = pd.DataFrame(letters, columns=["letter"])

df['cumcount'] = df.groupby('letter').cumcount()   1


df =

letter     cumcount  

  A           1
  A           2
  A           3
  B           1
  B           2
  B           3

What I'm looking to do is have the output be this.

df =

letter     cumcount    odd_count

  A           1            1
  A           2            3
  A           3            5
  B           1            1
  B           2            3
  B           3            5

CodePudding user response:

Lets do some math:

df['cumcount'] = df.groupby('letter').cumcount() * 2   1

  letter  cumcount
0      A         1
1      A         3
2      A         5
3      B         1
4      B         3
5      B         5
  • Related