remove only the first entries if they are zeros

Nelly Yuki :

I have a pandas series that looks like this:

df['column_3'].head(10)

0    0.0
1    0.0
2    0.0
3    0.0
4    0.0
5    0.1
6    0.0
7    0.1
8    0.1
9    0.0

I would like to remove only the first appearances of zeros, so in this example only rows 0 - 4. The number of zeros in the beginning of the series may vary and I only want to remove the zeros that come before the first instance of a non-zero entry. When I did this:

df[df['column_3'] != 0.0]

It removes all zeros, no matter the placement, which is not what I want.

I want it to look like this:

5    0.1
6    0.0
7    0.1
8    0.1
9    0.0

Any suggestions?

Thanks!

Quang Hoang :

cumsum is suitable for this situation:

df[df['column_3'].ne(0).cumsum().gt(0)]

Output:

   column_3        
5       0.1
6       0.0
7       0.1
8       0.1
9       0.0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23585&siteId=1