[Solved] How to transform given dataset in python? [closed]


From your expected result, it appears that each “group” is based on contiguous id values. For this, you can use the compare-cumsum-groupby pattern, and then use agg to get the min and max values.

# Sample data.
df = pd.DataFrame(
    {'id': [1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 1], 
     'A': [0.0, 6.4, 27.0, 27.1, 27.4, 27.7, 30.6, 31.0, 36.6, 36.9, 37.1, 37.1, 37.3, 37.8, 38.9, 39.2, 39.3, 39.5, 39.7, 42.6, 42.6, 42.8, 44.9, 45.6, 51.0],
     'B': [6.4, 27.0, 27.1, 27.4, 27.7, 30.6, 31.0, 36.6, 36.9, 37.1, 37.1, 37.3, 37.8, 38.9, 39.2, 39.3, 39.5, 39.7, 42.6, 42.6, 42.8, 44.9, 45.6, 51.0, 51.8]}
)

# Solution.
>>> (df
     .groupby(df['id'].ne(df['id'].shift()).cumsum())
     .agg({'id': 'first', 'A': 'min', 'B': 'max'})
     .set_index('id'))
       A     B
id            
1    0.0   6.4
2    6.4  30.6
1   30.6  39.7
2   39.7  42.8
1   42.8  45.6
2   45.6  51.0
1   51.0  51.8

solved How to transform given dataset in python? [closed]