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.
n : df Out : new_column 0 1 ten 20 3 1 4 0 5 1 6 1 In : df ["res"]= \ df.groupby (df ["new_column"]. eq (1) .cumsum ()) ["new_column"]. cumcount () In : df Out : new_column res 0 1 0 1 0 1 2 0 2 3 1 0 4 0 1 5 1 0 6 1 0
- 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)?