Reputation: 13
I'm trying to solve an issue related to set or replace values in rows by the value of the previous one in certain conditions. The data looks like:
Point_ID [1,2,3,1,2,1], Shape_ID [84,85,86,87,88,89]
I used mask and shift in conditions if Point_ID
starts with 1 the Shape_ID
of the second row should be replaced by the value of the previous row in case the number increases. If not then Shape_ID
should stay in a row. So I am trying to obtain:
Point_ID[1,2,3,1,2,1], Shape_ID[84,84,84,87,87,89]
Upvotes: 1
Views: 60
Reputation: 323226
Using eq,cumsum
df['GroupId']=df.Point_ID.eq(1).cumsum()
df.Shape_ID[df.Point_ID.ne(1)]=np.nan
df.assign(Shape_ID=df.groupby('GroupId')['Shape_ID'].ffill()).drop('GroupId',1)
Out[831]:
Point_ID Shape_ID
0 1 84.0
1 2 84.0
2 3 84.0
3 1 87.0
4 2 87.0
5 1 89.0
Upvotes: 0
Reputation: 153460
Use mask
, ne
, and ffill
:
df= pd.DataFrame({'Point_ID':[1,2,3,1,2,1] , 'Shape_ID': [84,85,86,87,88,89]})
print(df)
Point_ID Shape_ID
0 1 84
1 2 85
2 3 86
3 1 87
4 2 88
5 1 89
df.assign(Shape_ID=df['Shape_ID'].mask(df['Point_ID'].ne(1)).ffill().astype(int))
Output:
Point_ID Shape_ID
0 1 84
1 2 84
2 3 84
3 1 87
4 2 87
5 1 89
Upvotes: 2