【问题标题】:Pandas: check the unique values of a column with datatype 'set'Pandas:检查数据类型为“set”的列的唯一值
【发布时间】:2023-04-10 06:17:01
【问题描述】:

我有一个熊猫数据框my_dfmy_df 中的列 animals 具有数据类型“set”。然后我尝试使用下面的代码来检查animals这个列中有多少不同的值(集合):

print(my_df.animals.unique())

但出现以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-e41c02e0e954> in <module>()

     11 
---> 12 print(my_df.animals.unique())


/usr/local/lib/python3.4/dist-packages/pandas/core/series.py in unique(self)
   1237     @Appender(base._shared_docs['unique'] % _shared_doc_kwargs)
   1238     def unique(self):
-> 1239         result = super(Series, self).unique()
   1240         if is_datetime64tz_dtype(self.dtype):
   1241             # to return array of Timestamp with tz

/usr/local/lib/python3.4/dist-packages/pandas/core/base.py in unique(self)
    971         else:
    972             from pandas.core.nanops import unique1d
--> 973             result = unique1d(values)
    974         return result
    975 

/usr/local/lib/python3.4/dist-packages/pandas/core/nanops.py in unique1d(values)
    809     else:
    810         table = _hash.PyObjectHashTable(len(values))
--> 811         uniques = table.unique(_ensure_object(values))
    812     return uniques
    813 

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.unique (pandas/hashtable.c:14383)()

TypeError: unhashable type: 'set'

【问题讨论】:

    标签: python-3.x pandas dataframe set unique


    【解决方案1】:

    我不确定这是否正是你想要的,但你可以试一试:

    In [135]: df
    Out[135]:
         animals
    0     {1, 2}
    1  {1, 2, 3}
    2     {1, 2}
    
    In [136]: df.animals.astype(str).unique()
    Out[136]: array(['{1, 2}', '{1, 2, 3}'], dtype=object)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多