久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 91精品国产一区 | 老司机精品福利视频 | 日韩影院在线观看 | 成人在线不卡 | 一级片av | 手机看黄av免费网址 | 免费的日批视频 | 久久久久久蜜桃一区二区 | 国产视频一区在线 | 男人的天堂在线视频 | 中文字幕在线一区 | 一本在线| 国产精品69毛片高清亚洲 | 最新中文字幕在线 | 久久涩涩 | 亚洲精品一区二区三区免 | 亚洲精品大全 | 亚洲国产成人av好男人在线观看 | 亚洲精品乱码8久久久久久日本 | 日韩欧美网 | 一区二区国产在线 | 人人九九精 | 国产中文字幕在线 | 国产综合av | 久久久久无码国产精品一区 | 91精品一区 | 日韩中文字幕一区二区 | 国产精品亚洲一区 | 国产成人免费 | 久热精品在线观看视频 | 国产性色视频 | 亚洲成人福利 | 欧美一二三四成人免费视频 | 亚洲欧美国产精品久久 | 日韩欧美在线观看 | 久久毛片网站 | 日本网站在线看 | 精品国产一区二区三区久久久蜜月 | 七七婷婷婷婷精品国产 | 中文字幕一区二区三区四区五区 | 亚洲国产成人久久综合一区,久久久国产99 |