我有一個資料框df:
Cluster OsId BrowserId PageId VolumePred ConversionPred
0 11 11 {789615, 955761, 1149586, 955764, 955767, 1187... 147.0 71.0
1 0 11 12 {1184903, 955761, 1149586, 1158132, 955764, 10... 73.0 38.0
2 0 11 15 {1184903, 1109643, 955761, 955764, 1074581, 95... 72.0 40.0
3 0 11 16 {1123200, 1184903, 1109643, 1018637, 1005581, ... 7815.0 5077.0
4 0 11 17 {1184903, 789615, 1016529, 955761, 955764, 955... 52.0 47.0
... ... ... ... ... ... ...
307 {0, 4, 7, 9, 12, 15, 18, 21} 99 16 1154705 220.0 182.0
308 {18} 99 16 1155314 12.0 6.0
309 {9} 99 16 1158132 4.0 4.0
310 {0, 4, 7, 9, 12, 15, 18, 21} 99 16 1184903 966.0 539.0
這個資料框包含我需要洗掉它們的冗余行,所以我試試這個:
df.drop_duplicates()
但我收到了這個錯誤:TypeError: unhashable type: 'set'
有什么想法可以幫助我解決此錯誤嗎?謝謝!
uj5u.com熱心網友回復:
使用frozensets 來避免unhashable sets 型別,并使用反轉掩碼DataFrame.duplicated過濾:boolean indexing~
#sets are in any column
df1 = df.applymap(lambda x: frozenset(x) if isinstance(x, set) else x)
df[~df1.duplicated()]
如果沒有洗掉行,則表示沒有行有重復項(測驗的所有列都在一起)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/434028.html
標籤:Python python-3.x 熊猫 数据框
上一篇:從串列中構造一個pandasdf,ValueError:toomanyvaluestounpack(expected3)
