While working with the GroupBy concept I attempted to pivot my dataset using the unstack() function and encountered an AttributeError.
The DataFrame I have created is as follows:
df = pd.DataFrame({"key1": list("aabbab"),
"key2": list(["one", "two", "three"]*2),
"data1": np.random.randn(6),
"data2": np.random.randn(6)})
df
The purpose of the following code line is to group and pivot "data1" based on the "key1" and "key2" columns:
ort = df["data1"].groupby([df["key1"], df["key2"]]).mean()
ort.unstack()
However, when running this code, I encounter the error you see below:
AttributeError: 'SeriesGroupBy' object has no attribute 'unstack'
How can I overcome this issue?