Python 错误-不可损坏类型:';列表';使用多个值执行分组时
我对Python非常陌生,我一直在努力找出如何解决我在某些实例中看到的以下错误Python 错误-不可损坏类型:';列表';使用多个值执行分组时,python,pandas,dataframe,Python,Pandas,Dataframe,我对Python非常陌生,我一直在努力找出如何解决我在某些实例中看到的以下错误 for idKey, idKey_dataFrame in dataFrame[~pd.isna(dataFrame.idKey)].groupby("idKey", sort=False): idKey_dataFrame = idKey_dataFrame[idKey_dataFrame.matched_customerProps.astype(str).str.contains("entitlement")
for idKey, idKey_dataFrame in dataFrame[~pd.isna(dataFrame.idKey)].groupby("idKey", sort=False):
idKey_dataFrame = idKey_dataFrame[idKey_dataFrame.matched_customerProps.astype(str).str.contains("entitlement") & idKey_dataFrame.matched_customerProps.astype(str).str.contains("entitlement address")
]
for customer_type, idKey_sub_dataFrame in idKey_dataFrame.groupby("customer_type"):
if customer_type != “repeat” and customer_type != “mvp”:
continue
self.log.info(f”Link made to {idKey}")
idKey_csv_export = self.make_csv(idKey_sub_dataFrame, inventory=False)
我注意到,每当我有超过1个值时,我得到
“错误-不可损坏类型:'列表'”错误
我不知道该怎么做,所以当有多个值时,我会执行一个groupby,并与对象类型进行比较。任何帮助都将不胜感激
这些是我的专栏
customer_name customer_id customer_type
xyz 003 repeat
zzz 389 repeat, mvp, intl
yyy 002 repeat
yay 005 repeat
kdi 083 mvp, repeat
如果我提供单值customer\u类型,则不会出现错误。每当我在customer_类型下测试超过1个值的客户时,我都会得到一个错误。有问题的行是dataFrame[~pd.isna(dataFrame.idKey)
以下是完整的错误:
ERROR - unhashable type: 'list' in line 258, in execute for customer_type, idKey_sub_dataFrame in idKey_dataFrame.groupby("customer_type"):
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/ops.py", line 162, in get_iterator
splitter = self._get_splitter(data, axis=axis)
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/ops.py", line 168, in _get_splitter
comp_ids, _, ngroups = self.group_info
File "pandas/_libs/properties.pyx", line 34, in pandas._libs.properties.CachedProperty.__get__
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/ops.py", line 296, in group_info
comp_ids, obs_group_ids = self._get_compressed_labels()
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/ops.py", line 312, in _get_compressed_labels
all_labels = [ping.labels for ping in self.groupings]
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/ops.py", line 312, in <listcomp>
all_labels = [ping.labels for ping in self.groupings]
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/grouper.py", line 397, in labels
self._make_labels()
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/grouper.py", line 421, in _make_labels
labels, uniques = algorithms.factorize(self.grouper, sort=self.sort)
File "/usr/local/lib/python3.7/site-packages/pandas/util/_decorators.py", line 208, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/pandas/core/algorithms.py", line 672, in factorize
values, na_sentinel=na_sentinel, size_hint=size_hint, na_value=na_value
File "/usr/local/lib/python3.7/site-packages/pandas/core/algorithms.py", line 508, in _factorize_array
values, na_sentinel=na_sentinel, na_value=na_value
File "pandas/_libs/hashtable_class_helper.pxi", line 1798, in pandas._libs.hashtable.PyObjectHashTable.factorize
File "pandas/_libs/hashtable_class_helper.pxi", line 1718, in pandas._libs.hashtable.PyObjectHashTable._unique
错误-不可损坏类型:第258行中的“列表”,在idKey_数据帧中的“针对客户_类型执行”中,idKey_子_数据帧。groupby(“客户_类型”):
回溯(最近一次呼叫最后一次):
get_迭代器中的文件“/usr/local/lib/python3.7/site packages/pandas/core/groupby/ops.py”,第162行
拆分器=自身。\u获取\u拆分器(数据,轴=轴)
文件“/usr/local/lib/python3.7/site packages/pandas/core/groupby/ops.py”,第168行,在“获取”拆分器中
comp_id,u,ngroups=self.group_info
pandas.\u libs.properties.CachedProperty.\u获取__
文件“/usr/local/lib/python3.7/site packages/pandas/core/groupby/ops.py”,第296行,组信息中
comp_id,obs_group_id=self.\u获取\u压缩标签()
文件“/usr/local/lib/python3.7/site packages/pandas/core/groupby/ops.py”,第312行,在“获取压缩”标签中
所有标签=[ping.labels用于在self.groupings中ping]
文件“/usr/local/lib/python3.7/site packages/pandas/core/groupby/ops.py”,第312行,在
所有标签=[ping.labels用于在self.groupings中ping]
文件“/usr/local/lib/python3.7/site packages/pandas/core/groupby/grouper.py”,第397行,在标签中
self.\u制作标签()
文件“/usr/local/lib/python3.7/site packages/pandas/core/groupby/grouper.py”,第421行,在“make”标签中
标签,uniques=algorithms.factorize(self.grouper,sort=self.sort)
包装器中的文件“/usr/local/lib/python3.7/site packages/pandas/util/_decorators.py”,第208行
返回函数(*args,**kwargs)
文件“/usr/local/lib/python3.7/site packages/pandas/core/algorithms.py”,第672行,在factorize中
值,na_sentinel=na_sentinel,size_hint=size_hint,na_value=na_value
文件“/usr/local/lib/python3.7/site packages/pandas/core/algorithms.py”,第508行,在_factorize_数组中
值,na_sentinel=na_sentinel,na_值=na_值
文件“pandas/_libs/hashtable_class_helper.pxi”,第1798行,在pandas._libs.hashtable.PyObjectHashTable.factorize中
文件“pandas/_libs/hashtable_class_helper.pxi”,第1718行,在pandas._libs.hashtable.PyObjectHashTable._unique中
如果没有数据,甚至没有任何关于抛出该错误的部分的指示,则很难帮助您。我猜您尝试分组的列,无论是idKey
还是customer\u type
都是包含列表的列。该错误告诉您不能将列表或任何无法散列的类型用作分组列n、 我在您的数据集中没有看到idKey。而且您不能像这样迭代数据帧:id,df.groupby()中的df
:-这可能是导致错误的原因。始终共享整个错误消息,并使用完整错误更新。您是如何使用类型列表创建数据框列的?这不是一件小事,无论如何,groupby尝试哈希列的值,看看是否可以将列表转换为字符串、元组或集合