Используйте cumcount
для нового столбца counter
с set_index
, unstack
и reset_index
:
g = df.groupby(['gw_mac','mac','status']).cumcount()
df = (df.set_index([g, 'mac','status','gw_mac'])['val']
.unstack(fill_value=0)
.reset_index(level=[1,2])
.rename_axis(None, axis=1))
print (df)
mac status AC233FC01403 AC233FC015F6
0 AC233F264A4C Outwards -21 -37
1 AC233F264A4C Outwards -20 -37
2 AC233F264A4C Outwards -29 -39
3 AC233F264A4C Outwards -37 -37
4 AC233F264A4C Outwards -22 -37
5 AC233F264A4C Outwards 0 -37
Если важен порядок столбцов:
df = df[df.columns[2:].tolist() + df.columns[:2].tolist()]
print (df)
AC233FC01403 AC233FC015F6 mac status
0 -21 -37 AC233F264A4C Outwards
1 -20 -37 AC233F264A4C Outwards
2 -29 -39 AC233F264A4C Outwards
3 -37 -37 AC233F264A4C Outwards
4 -22 -37 AC233F264A4C Outwards
5 0 -37 AC233F264A4C Outwards