Issue
so i have a Dataframe that has a repeating Number Series that i want to group like this:
Number Pattern | Value | Desired Group | Value.1 |
---|---|---|---|
1 | 723 | 1 | Max of Group |
2 | 400 | 1 | Max of Group |
3 | 235 | 1 | Max of Group |
1 | 387 | 2 | Max of Group |
2 | 911 | 2 | Max of Group |
1 | 365 | 3 | Max of Group |
2 | 270 | 3 | Max of Group |
3 | 194 | 3 | Max of Group |
4 | 452 | 3 | Max of Group |
1 | 716 | 4 | Max of Group |
2 | 69 | 4 | Max of Group |
1 | 846 | 5 | Max of Group |
2 | 474 | 5 | Max of Group |
3 | 524 | 5 | Max of Group |
Any Ideas?
Solution
You can compare Number Pattern
by 1
with cumulative sum by Series.cumsum
and then is used GroupBy.transform
with max
:
df['Desired Group'] = df['Number Pattern'].eq(1).cumsum()
df['Value.1'] = df.groupby('Desired Group')['Value'].transform('max')
print (df)
Number Pattern Value Desired Group Value.1
0 1 723 1 723
1 2 400 1 723
2 3 235 1 723
3 1 387 2 911
4 2 911 2 911
5 1 365 3 452
6 2 270 3 452
7 3 194 3 452
8 4 452 3 452
9 1 716 4 716
10 2 69 4 716
11 1 846 5 846
12 2 474 5 846
13 3 524 5 846
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.