I have a Pandas dataframe with a column with groups of digits placed in ascending order:
column
1
2
3
4
5
6
0
1
2
3
4
1
2
3
4
5
0
0
I want to restructure those groups of sequences where the maximum > 4. The ascending order should remain the same but the maximum number must be 4. To do that I need to duplicate one or more middle digits. So I want the column to look like this:
column
1
2
2
2
3
4
0
1
2
3
4
1
2
2
3
4
0
0
Zeros should be left as they are.
I tried to, first, make the grouping variable:
k = 0
l = 1
i = 0
for k in range(1, len(df)):
if df.loc[k, 'column'] != 0:
if df.loc.loc[k, 'column'] > df.loc.loc[k-1, 'column']:
df.loc.loc[k, 'position'] = l
else:
l = l + 1
df.loc.loc[k, 'position'] = l
else:
df.loc.loc[k, 'position'] = 0
l = l + 1
l=pd.DataFrame(df.loc.groupby('position')['columns'].max()).reset_index()
And then I tried to come up with something like this but this does not work with different maximum values (not only 4):
z = 1
r = 0
for z in range (1, len(l)-1):
if l.loc[z,'column'] > 4:
for r in range(0, l.loc[z,'column'] - 3):
df.loc[df['column']==2+r, 'column'] = 2
df['column'] = np.where(df['column'] > 2, df['column'] - r, df['column'])
Please help!