Pandas Groupby错误

时间:2018-03-12 12:42:28

标签: python pandas pandas-groupby

尝试按月和国家/地区分组数据时,我收到以下错误:' tuple'对象没有属性' lower'

这是我正在使用的代码:

df = df.groupby(
[pd.to_datetime(df.time).dt.strftime('%b %Y'), 'Country'])['% Return'].mean().reset_index()

示例日期框架

time       Country  % Return

2017-07-30   br         3
2017-07-31   br         4
2017-08-01   br         5
2017-08-02   br         6
2017-08-03   br         7
2017-07-30   es         2
2017-07-31   es         3
2017-08-01   es         4
2017-08-02   es         5
2017-08-03   es         6

期望的输出:

time        Country  % Return
2017-07-01    br        3.5
2017-08-01    br        6
2017-07-01    es        2.5
2017-08-01    es        5

我对simialr DF使用了相同的代码。不知道为什么这次不工作

提前致谢

编辑:

python版本: Python 2.6.6 pandas版本: 0.22.0

完整错误

AttributeError                            Traceback (most recent call last)
<ipython-input-29-db720e55a304> in <module>()
      1 new_return_poster_df_g = new_return_poster_df_g.groupby(
----> 2     [pd.to_datetime(new_return_poster_df_g.time).dt.strftime('%b %Y'), 'Country']
      3 )['% Return Poster'].mean().reset_index(name='% Return Poster')

/var/local/ishbook.executor.daemon/lib/python-venvs/libraries/pandas==0.22.0/lib/python2.7/site-packages/pandas/core/tools/datetimes.pyc in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin)
    374         result = Series(values, index=arg.index, name=arg.name)
    375     elif isinstance(arg, (ABCDataFrame, MutableMapping)):
--> 376         result = _assemble_from_unit_mappings(arg, errors=errors)
    377     elif isinstance(arg, ABCIndexClass):
    378         result = _convert_listlike(arg, box, format, name=arg.name)

/var/local/ishbook.executor.daemon/lib/python-venvs/libraries/pandas==0.22.0/lib/python2.7/site-packages/pandas/core/tools/datetimes.pyc in _assemble_from_unit_mappings(arg, errors)
    444         return value
    445 
--> 446     unit = {k: f(k) for k in arg.keys()}
    447     unit_rev = {v: k for k, v in unit.items()}
    448 

/var/local/ishbook.executor.daemon/lib/python-venvs/libraries/pandas==0.22.0/lib/python2.7/site-packages/pandas/core/tools/datetimes.pyc in <dictcomp>((k,))
    444         return value
    445 
--> 446     unit = {k: f(k) for k in arg.keys()}
    447     unit_rev = {v: k for k, v in unit.items()}
    448 

/var/local/ishbook.executor.daemon/lib/python-venvs/libraries/pandas==0.22.0/lib/python2.7/site-packages/pandas/core/tools/datetimes.pyc in f(value)
    439 
    440         # m is case significant
--> 441         if value.lower() in _unit_map:
    442             return _unit_map[value.lower()]
    443 

AttributeError: 'tuple' object has no attribute 'lower'

1 个答案:

答案 0 :(得分:1)

我认为你可以使用pd.Grouper函数来获得所需的输出:

步骤1:转换为日期时间并将时间设置为索引

df['time'] = pd.to_datetime(df['time'])
df = df.set_index('time')

第2步:按时间和国家/地区分组

df = df.groupby([pd.Grouper(freq='M'),'Country'])['Return'].mean().reset_index()

    time       Country  Return
0   2017-07-31  br       3.5
1   2017-07-31  es       2.5
2   2017-08-31  br       6.0
3   2017-08-31  es       5.0