You can use:
a = df != 0
df1 = a.cumsum()-a.cumsum().where(~a).ffill().fillna(0).astype(int)
print (df1)
a b
0 0 1
1 1 2
2 0 3
3 1 0
4 2 1
5 0 2
More Related Contents:
- How to groupby consecutive values in pandas DataFrame
- Cumsum as a new column in an existing Pandas data
- Cumsum reset at NaN
- How can I use cumsum within a group in Pandas?
- Pandas dataframe – running sum with reset
- Cumsum as a new column in an existing Pandas dataframe
- Python pandas cumsum() reset after hitting max
- Why does the following code cant get value of n_fold = 1?
- Pandas long to wide reshape, by two variables
- How to avoid Python/Pandas creating an index in a saved csv?
- Convert floats to ints in Pandas?
- datetime dtypes in pandas read_csv
- Calculate average of every x rows in a table and create new table
- get previous row’s value and calculate new column pandas python
- How to move pandas data from index to column after multiple groupby
- sorting by a custom list in pandas
- Get last “column” after .str.split() operation on column in pandas DataFrame
- removing time from date&time variable in pandas?
- How do you update the levels of a pandas MultiIndex after slicing its DataFrame?
- Prevent pandas read_csv treating first row as header of column names
- How to specify date format when using pandas.to_csv?
- Using Pandas Value_Counts and matplotlib
- How to plot two columns of a pandas data frame using points
- What is the fastest way to output large DataFrame into a CSV file?
- Format a number with commas to separate thousands
- Python: UserWarning: This pattern has match groups. To actually get the groups, use str.extract
- Sorting the order of bars in pandas/matplotlib bar plots
- Collapsing rows in a Pandas dataframe if all rows have only one value in their columns
- Convert dates to pd.to_datetime where month could be either a number or month name
- Pandas: select all dates with specific month and day