<s> from pandas import * <EOL> import numpy as np <EOL> import string <EOL> import pandas . compat as compat <EOL> g1 = np . array ( list ( string . letters ) ) [ : - <NUM_LIT:1> ] <EOL> g2 = np . arange ( <NUM_LIT> ) <EOL> df_small = DataFrame ( { '<STR_LIT>' : [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:a>" , "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" , "<STR_LIT:c>" , "<STR_LIT:c>" , "<STR_LIT:c>" , <EOL> "<STR_LIT:c>" , "<STR_LIT:a>" , "<STR_LIT:a>" , "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:b>" , "<STR_LIT:b>" , "<STR_LIT:b>" ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> '<STR_LIT:value>' : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] } ) <EOL> value = df_small [ '<STR_LIT:value>' ] . values . repeat ( <NUM_LIT:3> ) <EOL> df = DataFrame ( { '<STR_LIT>' : g1 . repeat ( <NUM_LIT> * <NUM_LIT:5> ) , <EOL> '<STR_LIT>' : np . tile ( g2 , <NUM_LIT> * <NUM_LIT:5> ) , <EOL> '<STR_LIT:value>' : value . repeat ( <NUM_LIT> * <NUM_LIT:5> ) } ) <EOL> def random_sample ( ) : <EOL> grouped = df . groupby ( [ '<STR_LIT>' , '<STR_LIT>' ] ) [ '<STR_LIT:value>' ] <EOL> from random import choice <EOL> choose = lambda group : choice ( group . index ) <EOL> indices = grouped . apply ( choose ) <EOL> return df . reindex ( indices ) <EOL> def random_sample_v2 ( ) : <EOL> grouped = df . groupby ( [ '<STR_LIT>' , '<STR_LIT>' ] ) [ '<STR_LIT:value>' ] <EOL> from random import choice <EOL> choose = lambda group : choice ( group . index ) <EOL> indices = [ choice ( v ) for k , v in compat . iteritems ( grouped . groups ) ] <EOL> return df . reindex ( indices ) <EOL> def do_shuffle ( arr ) : <EOL> from random import shuffle <EOL> result = arr . copy ( ) . values <EOL> shuffle ( result ) <EOL> return result <EOL> def shuffle_uri ( df , grouped ) : <EOL> perm = np . r_ [ tuple ( [ np . random . permutation ( <EOL> idxs ) for idxs in compat . itervalues ( grouped . groups ) ] ) ] <EOL> df [ '<STR_LIT>' ] = np . asarray ( df . ix [ perm ] [ '<STR_LIT:value>' ] ) <EOL> df2 = df . copy ( ) <EOL> grouped = df2 . groupby ( '<STR_LIT>' ) <EOL> shuffle_uri ( df2 , grouped ) <EOL> df2 [ '<STR_LIT>' ] = grouped [ '<STR_LIT:value>' ] . transform ( do_shuffle ) </s>
