<jupyter_start><jupyter_text>```df.groupby('key1')['data1']df.groupby('key1')[['data2']]df.groupby(['key1', 'key2'])[['data2']] are syntactic sugar for:df['data1'].groupby(df['key1'])df[['data2']].groupby(df['key1'])df['data2'].groupby([df['key1'],df['key2']])```<jupyter_code>import numpy as np
import pandas as pd
PREVIOUS_MAX_ROWS = pd.options.display.max_rows
pd.options.display.max_rows = 20
np.random.seed(12345)
import matplotlib.pyplot as plt
plt.rc('figure', figsize=(10, 6))
np.set_printoptions(precision=4, suppress=True)
people = pd.DataFrame(np.random.randn(5, 5),
                      columns=['a', 'b', 'c', 'd', 'e'],
                      index=['Joe', 'Steve', 'Wes', 'Jim', 'Travis'])
people
people.index
index_key = pd.unique([len(x)for x in people.index]) #pd.array -> same result groupby as list below
# index_key = list(index_key) #list 
{len(x)for x in people.index} #set
list(people.iloc[:,0])
key_list = ['one', 'one', 'one', 'two', 'two']
[index_key, key_list]
#len : 3,5,3,3,6 
people.groupby([len, key_list]).mean()
#len : 3,5,3,3,6 
people.groupby([len, key_list]).min()
# key_list2 = ['two', 'two', 'two', 'one', 'two'] 
# key_list2 = ['one', 'two', 'two', 'two', 'two']
key_list2 = ['one', 'one', 'two', 'one', 'three'] #duplcated elements(one, one) grouped as single(one)
[index_key, key_list2] ##len : 3,5,3,3,6 
#len : 3,5,3,3,6 
people.groupby([len, key_list2]).min()
key_list2 = ['one', 'two', 'two', 'two', 'two']
people.groupby([len, key_list2]).min()
key_list2 = ['two', 'two', 'two', 'two', 'two']
people.groupby([len, key_list2]).min()
key_list2 = ['two', 'two', 'two', 'one', 'two']
people.groupby([len, key_list2]).min()
df = pd.DataFrame({"A":[1, 5, 3, 4, 2], 
                   "B":[3, 2, 4, 3, 4], 
                   "C":[2, 2, 7, 3, 4],  
                   "D":[4, 3, 6, 12, 7]}) 
df
df.quantile(.2, axis = 0) 
df['A'].mean()
df['A'].quantile(.5) 
tips = pd.read_csv('examples/tips.csv')
print(tips.shape)
tips.head()
tips['tip_pct'] = tips['tip']/tips['total_bill']
tips
def top(df, n=5, column='tip_pct'):
    return df.sort_values(by=column)[-n:]
tips.groupby(['smoker', 'day']).apply(top)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.24701011959521618, 'fraction_numerical': 0.03863845446182153, 'mean_word_length': 4.815508021390374, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '267352', 'license': 'MIT', 'n_tokens_mistral': 957, 'n_tokens_neox': 901, 'n_words': 177, 'path': 'ch10_test.ipynb', 'repo_name': 'blockchain99/data_analysis', 'size': '2174'}