Side Note:np.random.choice
如果没有参数replace = FALSE
,则可能会多次拾取一张卡,因为在提取对象后,该对象不会从 Select 池中移除.
考虑这个解决方案:
import pandas as pd
import numpy as np
# Dont worry about this. this is only to get some sample data
df = pd.DataFrame(np.transpose(np.array(
[[7,8,9,10, "j", "q", "k", "a"],
[7,8,9,10, "j", "q", "k", "a"],
[7,8,9,10, "j", "q", "k", "a"]])),
columns=['a', 'b', 'c'])
# end of data
# Choose two column names without replacement (dont pick same column twice or more)
random_colnames = np.random.choice(df.columns,2, replace = False)
# Custom colnames could be
#custom_colnames = ['a', 'b']
#drawn_cards = []
for col in random_colnames:
# pick elements from column with replacement
# and extend them to the list of already drawn cards
for _ in range(5):
# add replace = False if you do not want the same cardd picked more than once
draw = np.random.choice(df[col],4)
#drawn_cards.extend(draw)
print(draw)
# Print for showcase purpose
# print(drawn_cards)