久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Pandas drop_duplicates 方法不適用于包含列表的數據框

Pandas drop_duplicates method not working on dataframe containing lists(Pandas drop_duplicates 方法不適用于包含列表的數據框)
本文介紹了Pandas drop_duplicates 方法不適用于包含列表的數據框的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在我的數據幀上使用 drop_duplicates 方法,但我得到了一個錯誤.請參閱以下內容:

<塊引用>

錯誤:TypeError:不可散列的類型:'list'

我正在使用的代碼:

df = db.drop_duplicates()

我的數據庫很大,包含字符串、浮點數、日期、NaN、布爾值、整數...感謝任何幫助.

解決方案

如錯誤消息所示,drop_duplicates 不適用于數據框中的列表.但是,您可以在轉換為 str 的數據幀上刪除重復項,然后使用結果中的索引從原始 df 中提取行.

設置

df = pd.DataFrame({'Keyword': {0: 'apply', 1: 'apply', 2: 'apply', 3: 'terms', 4: 'terms'},'X': {0: [1, 2], 1: [1, 2], 2: 'xy', 3: 'xx', 4: 'yy'},'Y':{0:'yy',1:'yy',2:'yx',3:'ix',4:'xi'}})#Drop直接導致同樣的錯誤df.drop_duplicates()回溯(最近一次通話最后):...類型錯誤:不可散列類型:列表"

解決方案

#convert hte df 為 str 類型,刪除重復項,然后從原始 df 中選擇行.df.loc[df.astype(str).drop_duplicates().index]輸出[205]:關鍵字 X Y0 應用 [1, 2] 是2 應用 xy yx3 學期 xx ix4 學期 yy xi#列表元素在最終結果中仍然是列表.df.loc[df.astype(str).drop_duplicates().index].loc[0,'X']輸出[207]:[1, 2]

<塊引用>

將 iloc 替換為 loc.在這種特殊情況下,兩者都作為index 匹配位置索引,但不通用

I am trying to use drop_duplicates method on my dataframe, but I am getting an error. See the following:

error: TypeError: unhashable type: 'list'

The code I am using:

df = db.drop_duplicates()

My DB is huge and contains strings, floats, dates, NaN's, booleans, integers... Any help is appreciated.

解決方案

drop_duplicates won't work with lists in your dataframe as the error message implies. However, you can drop duplicates on the dataframe casted as str and then extract the rows from original df using the index from the results.

Setup

df = pd.DataFrame({'Keyword': {0: 'apply', 1: 'apply', 2: 'apply', 3: 'terms', 4: 'terms'},
 'X': {0: [1, 2], 1: [1, 2], 2: 'xy', 3: 'xx', 4: 'yy'},
 'Y': {0: 'yy', 1: 'yy', 2: 'yx', 3: 'ix', 4: 'xi'}})

#Drop directly causes the same error
df.drop_duplicates()
Traceback (most recent call last):
...
TypeError: unhashable type: 'list'

Solution

#convert hte df to str type, drop duplicates and then select the rows from original df.

df.loc[df.astype(str).drop_duplicates().index]
Out[205]: 
  Keyword       X   Y
0   apply  [1, 2]  yy
2   apply      xy  yx
3   terms      xx  ix
4   terms      yy  xi

#the list elements are still list in the final results.
df.loc[df.astype(str).drop_duplicates().index].loc[0,'X']
Out[207]: [1, 2]

Edit: replaced iloc with loc. In this particular case, both work as the index matches the positional index, but it is not general

這篇關于Pandas drop_duplicates 方法不適用于包含列表的數據框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美一区二区三区小说 | 国产精品激情在线 | 久久精品国产免费看久久精品 | 日韩一区二区免费视频 | 国产成人精品一区二区三区四区 | 国产精品自产拍在线观看蜜 | 欧美lesbianxxxxhd视频社区 | 日韩亚洲视频 | 在线综合视频 | 日韩一区二区免费视频 | 精品久久久久久久久久久下田 | 成人黄色三级毛片 | 欧美涩 | 中文字幕日韩av | 一级一级一级毛片 | 一道本不卡 | 久久久91精品国产一区二区精品 | 欧美一区视频 | 国产欧美一区二区三区日本久久久 | 国产91视频一区二区 | 亚洲精品在线视频 | 久久久tv | 二区久久 | 9191成人精品久久 | 国产精品久久久久久婷婷天堂 | 成人激情视频在线播放 | 亚洲成人免费网址 | av天天澡天天爽天天av | 男女免费在线观看视频 | 91正在播放 | 精品欧美乱码久久久久久 | 国产在线精品一区二区 | 米奇7777狠狠狠狠视频 | 日韩精品视频一区二区三区 | 中文字幕1区2区 | 国产精品综合久久 | 日韩小视频在线 | 亚洲欧美成人 | 97视频人人澡人人爽 | 国产精品久久在线 | 日韩精品一区二区三区四区视频 |