Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use Pandas to add a count with a certain interval to the entire csv file?

I have a conditional csv file and I want to add an interval count for example from 1 to 6 to get something like this:

A header Another header Another header
A 2 1
С 7 2
V 4 3
A 1 4
Q 1 5
V 2 6
N 8 1
A 2 2
L 4 3
A 6 4
S 4 5
F 3 6

Is it possible to implement this using Pandas ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use vectorial code for efficiency, numpy is the perfect tool:

import numpy as np

N = 6
df['Another header'] = np.arange(len(df))%N+1

output:

   A header   Another header   Another header
0         A                 2               1
1         С                 7               2
2         V                 4               3
3         A                 1               4
4         Q                 1               5
5         V                 2               6
6         N                 8               1
7         A                 2               2
8         L                 4               3
9         A                 6               4
10        S                 4               5
11        F                 3               6
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading