pandas groupby rank返回ValueError

时间:2017-02-17 20:59:03

标签: python pandas group-by

我的df:

{'a': {0: 548.12,
  1: 111.34999999999999,
  2: 64.840000000000003,
  3: 63.810000000000002,
  4: 614.28999999999996,
  5: -207.49000000000001,
  6: 151.31999999999999,
  7: -56.43,
  8: 611.37,
  9: -296.62,
  10: 6417.5699999999997,
  11: -376.25999999999999,
  12: 465.12,
  13: -821.73000000000002,
  14: 1270.6700000000001,
  15: -1410.0899999999999,
  16: 1312.6600000000001,
  17: -326.25999999999999,
  18: 1683.3699999999999,
  19: -24.440000000000001,
  20: 583.60000000000002,
  21: -5.2400000000000002,
  22: 1122.74,
  23: 195.21000000000001,
  24: 338.27999999999997,
  25: -129.69},
 'dest': {0: 'SFO',
  1: 'PDX',
  2: 'SFO',
  3: 'PDX',
  4: 'SFO',
  5: 'PDX',
  6: 'SFO',
  7: 'PDX',
  8: 'SFO',
  9: 'PDX',
  10: 'SFO',
  11: 'PDX',
  12: 'SFO',
  13: 'PDX',
  14: 'SFO',
  15: 'PDX',
  16: 'SFO',
  17: 'PDX',
  18: 'SFO',
  19: 'PDX',
  20: 'SFO',
  21: 'PDX',
  22: 'SFO',
  23: 'PDX',
  24: 'SFO',
  25: 'PDX'},
 'src': {0: 'YYZ',
  1: 'DFW',
  2: 'YYZ',
  3: 'DFW',
  4: 'YYZ',
  5: 'DFW',
  6: 'YYZ',
  7: 'DFW',
  8: 'YYZ',
  9: 'DFW',
  10: 'YYZ',
  11: 'DFW',
  12: 'YYZ',
  13: 'DFW',
  14: 'YYZ',
  15: 'DFW',
  16: 'YYZ',
  17: 'DFW',
  18: 'YYZ',
  19: 'DFW',
  20: 'YYZ',
  21: 'DFW',
  22: 'YYZ',
  23: 'DFW',
  24: 'YYZ',
  25: 'DFW'}}

注意:

Above dct can be reconstructed to a dataframe via:

import pandas as pd
from pandas import Timestamp

df = pd.DataFrame(dct)

我想现在按a

排列group = ['dest','src']

我按照此帖[{3}}

中的解决方案进行操作
df.groupby(['dest','src'])['a'].rank(pct=True)

OR

df.groupby(['dest','src']).a.rank(pct=True)

但结果是:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-69-84e9b648c3fb> in <module>()
----> 1 new_df.groupby(['dest','src'])['a'].rank(pct=True)

/Users/blah1/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in rank(self, axis, method, numeric_only, na_option, ascending, pct)

/Users/blah1/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in wrapper(*args, **kwargs)
    590                                                             *args, **kwargs)
    591                     except (AttributeError):
--> 592                         raise ValueError
    593 
    594         return wrapper

ValueError: 

更新:

python版本

2.7.12 |Anaconda 2.3.0 (x86_64)| (default, Jul  2 2016, 17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)]
In [50]:

Pandas版

0.19.2

0 个答案:

没有答案