简体   繁体   中英

Python Pandas groupby and qcut doesn't work in 0.14.1

I would like to bin the observations by group and put the bin assignment back into the dataframe:

In [60]: df = pd.DataFrame({'x': np.random.rand(20), 'grp': ['a'] * 10 + ['b'] * 10})

In [61]: df['y'] = df.groupby('grp')['x'].transform(pd.qcut, 3)
Traceback (most recent call last):

  File "<ipython-input-61-fe2f09d0bbe2>", line 1, in <module>
    df.groupby('grp')['x'].transform(pd.qcut, 3)

  File "C:\Python\python-2.7.6.amd64\lib\site-packages\pandas\core\groupby.py", line 2286, in transform
    result[indexer] = res

ValueError: could not convert string to float: (0.352, 0.784]

The code above used to work in 0.13 I believe, but now it does not seem to work in 0.14.1. Any idea?

这是一种解决方法,将由qcut创建的Categorical包装为一系列。

df['y'] = df.groupby('grp')['x'].apply(lambda x: pd.Series(pd.qcut(x,3), index=x.index))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM