0

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?

eesc
  • 1
  • 2
  • What is the expected output? Are you looking for something like `df.pivot_table(index='key1', columns='key2', values='data1', aggfunc='mean', fill_value=0)` – Corralien May 16 '23 at 13:33
  • @Corralien Yes, this is what i expected. – eesc May 16 '23 at 13:43
  • Take care about the aggregate function. I choose `'mean'` here but you can prefer `'sum'` or other functions. – Corralien May 16 '23 at 13:44
  • @Corralien correct but I don't understand what this has to do with my problem. – eesc May 16 '23 at 13:54
  • As you have duplicate keys, I mean multiple values for a same (key1, key2), you have to take a decision about values. Suppose you have 2 values for ('a', 'one'): 4 and 2. Pandas can insert ONE AND ONLY ONE value in the cell. Which one? 4 (the first), 2 (the last), 2 (the min), 4 (the max), 6 (the sum), 3 (the mean), etc. – Corralien May 16 '23 at 13:57
  • @Corralien Got it, I chose the mean( ) function. – eesc May 16 '23 at 14:20
  • Perfect. I closed your post as it's a dupe of [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe). Take a while to read it. – Corralien May 16 '23 at 15:05

0 Answers0