I faced such a problem. In the picture, there is a new_column in which a one appears when a sequence breaks in the period_id column. I need to create another column in which the number of periods that are in the range from one unit to another will be sequentially numbered. Those. 0,1,2,3 ... 15, then the numbering sees one and starts over (0,1), etc. The question is, is it possible to register this task using the cumsum function? I tried summing only zeros, but I don't understand how to make sure that when a new zero appears after one, the numbering starts over.
df ['final']= df ['new_column']. eq (0) .cumsum ()

Answer # 1
This task can be solved using the vectorized (i.e., without resorting to loops) Pandas tools.
Example:
n [229]: df Out [229]: new_column 0 1 ten 20 3 1 4 0 5 1 6 1 In [230]: df ["res"]= \ df.groupby (df ["new_column"]. eq (1) .cumsum ()) ["new_column"]. cumcount () In [231]: df Out [231]: new_column res 0 1 0 1 0 1 2 0 2 3 1 0 4 0 1 5 1 0 6 1 0
Thank you very much, very cool solution!)
Marina Bazyleva20210223 18:29:32Well, groupby here, of course, suggested itself, but I did not guess further)
CrazyElf20210223 18:29:32@CrazyElf, EMNIP it only exists for GropBy objects
MaxU20210223 18:29:32Elegantly! I broke my whole head, did not know what cumcount is)
CrazyElf20210223 18:29:32
 python : How do I translate columns to rows in pandas?
 python : How to import image into SQLite
 python : How to read Excel spreadsheets with a compound title?
 python : How to conditionally replace values in a DataFrame column with values from another DataFrame
 python : Get elements of a specific column of a dataframe
 python : How does sort_values work for multiple columns in Pandas?
 python : How to change column and label values in txt file?
 python : How to change column type in DataFrame
 python : Merge two columns
 python : Filter specific rows with year in a dataframe (pandas)?
Please provide in the question a small but reproducible example of input data (in the form of text /CSV /Python code or a link to a file) and what you expect to receive in the output. I also advise you to read: How to most effectively ask a question related to data processing and /or analysis (for example: by Pandas /Numpy /SciPy /SciKit Learn /SQL)
MaxU20210223 18:29:32