<jupyter_start><jupyter_text>Lecture 10   Apply with Multiple Columns<jupyter_code>temperatures = Table.read_table('temperatures.csv')
temperatures
temperatures.plot('Day')
temperatures.select('Low', 'High').hist(bins=np.arange(30, 105, 5))
temperatures.scatter('Low', 'High')
# Difference between high temp and low temp

def difference(x, y):
    return x-y

difference(65, 54)
daily_spread = temperatures.apply(difference, 'High', 'Low')
temperatures = temperatures.with_column('Spread', daily_spread)
temperatures
temperatures.hist('Spread', bins=np.arange(0, 40, 4))
temperatures.where('Spread', are.above(20)).num_rows / temperatures.num_rows<jupyter_output><empty_output><jupyter_text>Function with Optional Arguments<jupyter_code>def percents(s, places):
    return np.round(s/sum(s) * 100, places)
x = make_array(2, 5, 16)
percents(x, 4)
def percents(s, places=2):
    return np.round(s/sum(s) * 100, places)
percents(x)<jupyter_output><empty_output><jupyter_text>Grouping by Category<jupyter_code>all_cones = Table.read_table('cones.csv')
all_cones
cones = all_cones.drop('Color').exclude(5)
cones
cones.group('Flavor')
cones.group('Flavor', min)
cones.group('Flavor', list)
cones.group('Flavor', np.average)
cones.group('Flavor', min)
min(cones.where('Flavor', 'chocolate').column('Price'))
def spread(arr):
    return max(arr) - min(arr)

spread(make_array(7, 10, 2))
cones.group('Flavor', spread)
cones
all_cones
all_cones.group(['Flavor', 'Color'])
all_cones.group(['Flavor', 'Color'], np.average)<jupyter_output><empty_output><jupyter_text>Examples<jupyter_code>nba = Table.read_table('nba_salaries.csv').relabeled(3, 'SALARY')
nba
# total salary paid by each team, highest first

nba.select('TEAM', 'SALARY').group('TEAM', sum).sort('SALARY sum', descending=True)
nba.group('TEAM', sum)
# average salary paid for each position

nba.select('POSITION', 'SALARY').group('POSITION', np.average)
# for each team, average salary paid for each position

nba.drop('PLAYER').group(['TEAM', 'POSITION'], np.average)<jupyter_output><empty_output><jupyter_text>Pivot Tables<jupyter_code>all_cones
all_cones.group(['Flavor', 'Color'])
all_cones.pivot('Flavor', 'Color')
all_cones.pivot('Flavor', 'Color', values='Price', collect=np.average)<jupyter_output><empty_output><jupyter_text>Examples<jupyter_code>survey = Table.read_table('welcome_survey.csv')
survey.show(3)
survey.pivot('Pant leg order', 'Handedness')
nba
# for each team, average salary paid for each position

nba.pivot('POSITION', 'TEAM', values = 'SALARY', collect = np.average)
# CHALLENGE QUESTION: for each team,
# amount paid to "starter" (player earning the most) in each position<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1595229221021245, 'fraction_numerical': 0.013790532985464033, 'mean_word_length': 8.098305084745762, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '76412', 'license': 'CC0-1.0', 'n_tokens_mistral': 1044, 'n_tokens_neox': 986, 'n_words': 188, 'path': 'lec/lec10/lec10.ipynb', 'repo_name': 'schoend/tu-fa21', 'size': '2683'}