3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
numpy.unique
與length
唯一值一起使用:
out?=?len(np.unique(df)) 6

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
用于NumPy
此,如:
import numpy as np print(np.unique(df.values).shape[0])

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊
您也可以使用set, :lenflatten
len(set(df.values.flatten()))
出去:
6
計(jì)時(shí):使用具有 6 個(gè)唯一值的虛擬數(shù)據(jù)框
#dummy data
df = pd.DataFrame({'Day':np.random.choice(['aa','bbbb','c','ddddd','EeeeE','xxx'], 10**6),'Heloo':np.random.choice(['aa','bbbb','c','ddddd','EeeeE','xxx'], 10**6)})
print(df.shape)
(1000000, 2)
%timeit len(set(df.values.flatten()))
>>>89.5 ms ± 1.56 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit np.unique(df.values).shape[0]
>>>1.61 s ± 25.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit len(np.unique(df))
>>>1.85 s ± 229 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
添加回答
舉報(bào)