2

I am using Pycharm (version 2018.2.4) with Python 3.6.7 running on it.

I currently try to use the pandas pivot function, but even the sample code:

import pandas as pd
df = pd.DataFrame({'foo':['one', 'one', 'one', 'two', 'two','two'],'bar': 
  ['A', 'B', 'C', 'A', 'B', 'C'],'baz': [1, 2, 3, 4, 5, 6],'zoo': ['x', 
   'y', 'z', 'q', 'w', 't']})
df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])

leads to the error: Exception: Data must be 1-dimensional

This is only the case when I pass a list of columns to the values parameter, however this example is taken straight from the function help: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.pivot.html

It used to work for me in the past and has stopped working this week, not sure why. My pandas version is 0.23.4 and numpy version is 1.15.4.

Does anybody know what is causing this/how to fix it?

Thanks!

yatu
  • 86,083
  • 12
  • 84
  • 139
stookie
  • 41
  • 1
  • 3
  • I am not able to reproduce this with v0.23.4. – ayhan Jan 14 '19 at 17:21
  • Could it have to do with numpy? I suppose this is using a numpy array of some sort in the background and I also had another error today when trying to apply map function to another pandas time index (df I use is named prices): The code prices.index.strftime('%j').map(lambda x: math.ceil(int(x) / 7.0)) is causing AttributeError: 'numpy.ndarray' object has no attribute 'map' Used to work fine too up to last week – stookie Jan 14 '19 at 17:29

3 Answers3

2

I was able to recreate the same error when testing in jupyter notebook with pandas: 0.22.0 and numpy: 1.14.0.

I wasn't able to figure out why this issue happens but I rewrote the code in the example as

import pandas as pd
df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
                          'two'],
                  'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
                   'baz': [1, 2, 3, 4, 5, 6],
                   'zoo': ['x', 'y', 'z', 'q', 'w', 't']})

df.set_index(['foo','bar'],inplace=True)
df.unstack(level=1)

and got the expected output (as seen in the pandas doc )

Hope this helps!

Meghana
  • 31
  • 5
1

The following works for me. Give it a try!

import pandas as pd
df = pd.DataFrame({'foo':['one', 'one', 'one', 'two', 'two','two'],'bar': 
  ['A', 'B', 'C', 'A', 'B', 'C'],'baz': [1, 2, 3, 4, 5, 6],'zoo': ['x', 
   'y', 'z', 'q', 'w', 't']})
df.pivot(index='foo', columns='bar')[['baz', 'zoo']]
Kungfu_panda
  • 411
  • 4
  • 7
0

If you make it a dictionary, it could work fine. Try this!

df = pd.DataFrame({'foo':['one', 'one', 'one', 'two', 'two','two'],'bar': 
  ['A', 'B', 'C', 'A', 'B', 'C'],'baz': [1, 2, 3, 4, 5, 6],'zoo': ['x', 
   'y', 'z', 'q', 'w', 't']})
df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])

#output:

    baz zoo
bar A   B   C   A   B   C
foo                     
one 1   2   3   x   y   z
two 4   5   6   q   w   t
Venkatachalam
  • 16,288
  • 9
  • 49
  • 77
  • Unfortunately that does not solve it for me, it gives me the same error as before. – stookie Jan 14 '19 at 17:17
  • It's very strange, it used to work just fine for me too, I can't really think of anything that changed and could be causing it. – stookie Jan 14 '19 at 17:22