两种方法:
from pandas import *
import numpy as np
a = np.array([[1, 1,1], [2, 3,1], [1, 1,1], [5, 4,2], [2, 3,1]])
print( DataFrame(a).drop_duplicates().values)
def delrepeat(a):
# a = np.array([[1, 1, 1], [2, 3, 1], [1, 1, 1], [5, 4, 2], [2, 3, 1]])
# a=a.tolist()
ll2=[]
for i in a:
# if isinstance(i,list):
# if delrepeat(i) not in ll2:
# ll2.append(delrepeat(i))
if i not in ll2:
ll2.append(i)
return ll2
print(delrepeat(a.tolist()))
一维去重复,不支持多维
a = [27, 7, 12, 5, 27, 14, 7, 14, 5, 14, 5];
print(unique(a))
多维只是找到不重复的元素
import numpy as np
a=[[1, 1, 1], [2, 3, 1], [1, 1, 1], [5, 4, 2], [2, 3, 1]]
print(np.unique(a))
这个有好多案例:
df = pd.DataFrame({'Col1': ['Bob', 'Joe', 'Bill', 'Mary', 'Joe'], = pd.DataFrame({'Col1': ['Bob', 'Joe', 'Bill', 'Mary', 'Joe'],
'Col2': ['Joe', 'Steve', 'Bob', 'Bob', 'Steve'],'Col2': ['Joe', 'Steve', 'Bob', 'Bob', 'Steve'],
'Col3': np.random.random(5)})'Col3': np.random.random(5)})
pd.unique(df[['Col1', 'Col2']].values.ravel('K')).unique(df[['Col1', 'Col2']].values.ravel('K'))
array(['Bob', 'Joe', 'Bill', 'Mary', 'Steve'], dtype=object)(['Bob', 'Joe', 'Bill', 'Mary', 'Steve'], dtype=object)
1388

被折叠的 条评论
为什么被折叠?



