I have the following df:
index | jan | feb | marc | april |
---|---|---|---|---|
One | 1 | 7 | 0 | 0 |
two | 0 | 8 | 7 | 0 |
three | 0 | 0 | 0 | 1 |
I'd like to get the number of zeros after the last non-zero value per row. So the output should look like
index | num |
---|---|
One | 2 |
two | 1 |
three | 0 |
import pandas as pd
# Dataframe
data = {
'jan': [1, 0, 0],
'feb': [7, 8, 0],
'marc': [0, 7, 0],
'april': [0, 0, 1]
}
df = pd.DataFrame(data, index=['One', 'two', 'three'])
# Calculate the number of zeros after the last non-zero value per index/row
num_zeros = df.ne(0).iloc[:, ::-1].cumsum(axis=1).eq(0).sum(axis=1)
# Result dataframe
result = pd.DataFrame({'num': num_zeros}, index=df.index)
print("DataFrame:")
print(df)
print("\nResult:")
print(result)
Calculating the # of zeroes (df.ne(0).iloc[:, ::-1].cumsum(axis=1).eq(0).sum(axis=1)):
Prints:
DataFrame:
jan feb marc april
One 1 7 0 0
two 0 8 7 0
three 0 0 0 1
Result:
num
One 2
two 1
three 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With