fu xue
fu xue

Reputation: 401

pandas fill value between the index of two rows(rows only find by a value)

I have a Series df:

index 
0     1
1     1
2     1
3     1
4     1
5    -1
6    -1
7    -1
8     1
9     1
10    1
11   -1
dtype: int64

Another boolean Series is likes indicator or points b:

index 
0     False
1     False
2     True
3     False
4     False
5     False
6     True
7     False
8     False
9     True
10    False
11    False

I can set the df values using b, df[b]=0:

index 
0     1
1     1
2     0
3     1
4     1
5    -1
6     0
7    -1
8     1
9     0
10    1
11   -1

And now what I want to fill the values between 2:5,6:7,9:11 with the value -1 and the result I want is a new df:

index 
0     1
1     1
2    -1
3    -1
4    -1
5    -1
6    -1
7    -1
8     1
9    -1
10   -1
11   -1 

Which means when b is True, (index:2,6,9), I would fill the value 1 in df between the index(index:2,6,9) and the index of the nearest -1 values (index:5,7,11).

The fill value is -1, the fill range is [2:5,6:7,9:11]

I've thought method like where, replace, pad etc, but cannot work it out. Maybe find its index array [2,6,9],and the nearest -1 array [5,7,11], and rearrange into [2:5,6:7,9:11] is a way.

Is there some ways more useful?

Upvotes: 1

Views: 2403

Answers (1)

Stephen Rauch
Stephen Rauch

Reputation: 49784

numpy.where() looks like it can do what you need:

Code:

import numpy as np

starts = np.where(df == 0)
ends = np.where(df == -1)
for start, end in zip(starts[0], ends[0]):
    df[start:end] = -1

Test Data:

import pandas as pd
df = pd.DataFrame([1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1])
b = pd.DataFrame([False, False, True, False, False, False, True,
                  False, False, True, False, False,])
df[b] = 0
print(df)

Results:

    0
0   1
1   1
2  -1
3  -1
4  -1
5  -1
6  -1
7  -1
8   1
9  -1
10 -1
11 -1

Upvotes: 2

Related Questions