I am trying to use a groupby on a dataframe as described here: Groupby and lag all columns of a dataframe?
For that I created this piece of code:
import pandas as pd
import numpy as np
data=range(1,101)
df=pd.DataFrame(data=data)
df.columns=["a"]
df["b"]=np.round( df["a"] / 5)
print(df)
shifted = df.groupby(level="b").shift(-1)
df=df.join(shifted)
print(df)
Unfortunatly this throws a "ValueError: level name b is not the name of the index" error and I cannot seem to figure out why. Printing the df shows that it has only a single level of indexes with "b" being in it.
Anyone know how to get this to work?
PS: I am trying to get a shift on certain columns but on group level.