如何在没有重复数组的情况下获得结果?
How to get results without duplicates array?
如何得到不重复的结果?
我在下面的描述中显示了一个 CSV 文件,我将所有列放在一起,以从所有列中获得随机结果,但我得到的结果重复。
谢谢
test.csv
d c b a
-----------------
0 Q A A Q
1 K A K 8
2 8 10 8 10
import pandas as pd
import numpy as np
df = pd.read_csv('test.csv', usecols=['a','b','c','d'])
df = np.array(df.iloc[0:3])
np.random.seed(2)
print(np.random.choice(df.flatten(), size=(20, 2)))
结果
[['8' '8'] # duplicate
['K' '10']
['A' '10']
['8' '8'] # duplicate
['A' 'A']
['10' 'A'] # duplicate
['8' 'K'] # duplicate
['K' 'A']
['8' 'Q']
['K' 'K']
['8' '10']
['Q' '8']
['K' '8']
['A' '8']
['Q' 'A']
['8' 'K'] # duplicate
['K' 'Q']
['10' 'A'] # duplicate
['Q' 'K']
['A' 'K']]
使用 DataFrame.duplicated
和反转掩码 ~
:
a = np.random.choice(df.flatten(), size=(20, 2))
a2 = a[~pd.DataFrame(a).duplicated(keep=False)]
print (a2)
['Q' '8']
['K' '8']
['A' '8']
['Q' 'A']
['K' 'Q']
['Q' 'K']
['A' 'K']]
详情:
print (pd.DataFrame(a).duplicated(keep=False))
0 True
1 False
2 False
3 True
4 False
5 True
6 True
7 False
8 False
9 False
10 False
11 False
12 False
13 False
14 False
15 True
16 False
17 True
18 False
19 False
dtype: bool
如何得到不重复的结果? 我在下面的描述中显示了一个 CSV 文件,我将所有列放在一起,以从所有列中获得随机结果,但我得到的结果重复。
谢谢
test.csv
d c b a
-----------------
0 Q A A Q
1 K A K 8
2 8 10 8 10
import pandas as pd
import numpy as np
df = pd.read_csv('test.csv', usecols=['a','b','c','d'])
df = np.array(df.iloc[0:3])
np.random.seed(2)
print(np.random.choice(df.flatten(), size=(20, 2)))
结果
[['8' '8'] # duplicate
['K' '10']
['A' '10']
['8' '8'] # duplicate
['A' 'A']
['10' 'A'] # duplicate
['8' 'K'] # duplicate
['K' 'A']
['8' 'Q']
['K' 'K']
['8' '10']
['Q' '8']
['K' '8']
['A' '8']
['Q' 'A']
['8' 'K'] # duplicate
['K' 'Q']
['10' 'A'] # duplicate
['Q' 'K']
['A' 'K']]
使用 DataFrame.duplicated
和反转掩码 ~
:
a = np.random.choice(df.flatten(), size=(20, 2))
a2 = a[~pd.DataFrame(a).duplicated(keep=False)]
print (a2)
['Q' '8']
['K' '8']
['A' '8']
['Q' 'A']
['K' 'Q']
['Q' 'K']
['A' 'K']]
详情:
print (pd.DataFrame(a).duplicated(keep=False))
0 True
1 False
2 False
3 True
4 False
5 True
6 True
7 False
8 False
9 False
10 False
11 False
12 False
13 False
14 False
15 True
16 False
17 True
18 False
19 False
dtype: bool