对数据帧进行排序和删除重复项时出现Pandas顺序错误

2024-05-29 05:03:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用以下代码:

UsersFullUnique = UsersFullLoc
UsersFullUnique.Placetype = UsersFullUnique.Placetype.astype('category', 
                                    categories=['Continent', 'Country', 'State', 'County','Town','POI', 'Suburb', 'LocalAdmin', 'Island', 'Estate', 'Colloquial', 'HistoricalTown', 'HistoricalCounty', 'LandFeature', 'Supername'], 
                                    ordered=True)

UsersFullUnique = UsersFullUnique.sort_values('Placetype').groupby('ID', as_index=False).first()
UsersFullUnique.head(8)

对以下数据帧进行排序:

^{pr2}$

这给了我一个错误

TypeError: _astype() got an unexpected keyword argument 'ordered'

奇怪的是,我在另外两个带索引的数据集上使用了这段代码

Unnamed: 0  WOE_ID  Locationname_x  Name_Type   Language_x  ID  Username    Friends Followers   Status_count    Favorites   Account_age ISO Locationname_y  Language_y  Placetype   Parent_ID

以及

WOE_ID  ISO Locationname    Language    Placetype   Parent_ID   ID  Username    Friends Followers   Status_count    Favorites   Account_age

它包含了几乎相同类型的信息,而且没有出错。在

有人知道可能的解决办法吗?在


Tags: 数据代码idstatuscountusernamelanguageordered
1条回答
网友
1楼 · 发布于 2024-05-29 05:03:45

它可能是一个类似于this one的bug。直接的解决方案是确保该列不是一个类别:

if UsersFullUnique.Placetype.dtype != 'category': 
    UsersFullUnique.Placetype = UsersFullUnique.Placetype.astype('category', 
                                    categories=[...], 
                                    ordered=True)

更普遍的问题是,使用UsersFullUnique = UsersFullLoc时,您不会复制一个副本,而只是给同一个对象指定两个名称,因此对新数据帧所做的任何更改也将对旧数据帧进行。在

如果出于某种原因需要复印件,应使用:

^{pr2}$

相关问题 更多 >

    热门问题