在“熊猫v 012”中,我有下面的数据。
import pandas as pd
df = pd.DataFrame({'id' : range(1,9),
'code' : ['one', 'one', 'two', 'three',
'two', 'three', 'one', 'two'],
'colour': ['black', 'white','white','white',
'black', 'black', 'white', 'white'],
'texture': ['soft', 'soft', 'hard','soft','hard',
'hard','hard','hard'],
'shape': ['round', 'triangular', 'triangular','triangular','square',
'triangular','round','triangular'],
'amount' : np.random.randn(8)}, columns= ['id','code','colour', 'texture', 'shape', 'amount'])我可以“群由”code,如下所示:
c = df.groupby('code')但是,如何才能得到与code有关的独特的code发生呢?我试过这个错误:
question = df.groupby('code').agg({'texture': pd.Series.unique}).reset_index()
#error: Must produce aggregated value从上面给出的df中,我希望结果是一本字典,具体来说就是这个:
result = {'one':['soft','hard'], 'two':['hard'], 'three':['soft','hard']}我的实际df的大小相当大,所以我需要解决方案的效率和速度。
发布于 2015-03-09 16:46:57
获得唯一值字典的一种方法是将pd.unique应用于groupby对象:
>>> df.groupby('code')['texture'].apply(pd.unique).to_dict()
{'one': array(['hard', 'soft'], dtype=object),
'three': array(['hard', 'soft'], dtype=object),
'two': array(['hard'], dtype=object)}在新版本的熊猫中,unique是groupby对象的一种方法,因此更整洁的方法是:
df.groupby("code")["texture"].unique()https://stackoverflow.com/questions/28947223
复制相似问题