ビッグデータからのランダムサンプリングと配列---groupbyとapplyの応用
5790 ワード
トランプを作って、その中の何枚かをランダムに抽出します.
import numpy as np
from pandas import DataFrame,Series
# hearts spades clubs diamonds
#
suits = ['h','s','c','d']
# [10]*3 3 10, 4
card_val = (list(range(1,11)) + [10] * 3 ) * 4
#
base_names = ['A'] + list(range(2,11)) + ['J','Q','K']
cards = []
# ( )
for suit in suits:
cards.extend(str(num) + suit for num in base_names)
deck = Series(card_val,index=cards)
# 52 n
def draw(deck,n):
# N K
return deck.take(np.random.permutation(len(deck))[:n])
get_suit = lambda card:card[-1]# ,
#
print(deck.groupby(get_suit).apply(draw,n=2))