<jupyter_start><jupyter_text>Coding Exercise 0208  1. MultiIndex:<jupyter_code>import pandas as pd
import numpy as np
import os<jupyter_output><empty_output><jupyter_text>1.1. Creating a DataFrame with MultiIndex:<jupyter_code>my_header = ['A','B','C']
my_index_out = ['G1']*2 + ['G2']*2 + ['G3']*2
my_index_in = ['a', 'b']*3
my_index_zipped = list(zip(my_index_out, my_index_in))
my_index_zipped
my_multi_index = pd.MultiIndex.from_tuples(my_index_zipped)
df = pd.DataFrame(data=np.random.randn(6,3),index=my_multi_index,columns=my_header)
df<jupyter_output><empty_output><jupyter_text>1.2. Indexing and slicing DataFrames with MultiIndex:<jupyter_code>df.loc['G1']
# Indexing followed by another indexing.
df.loc['G1'].loc['a']
# Indexing followed by another indexing.
df.loc['G1'].loc['a','B']<jupyter_output><empty_output><jupyter_text>2. DataFrame summarization:  2.1. Group by method:<jupyter_code># Go to the directory where the data file is located. 
os.chdir(r'~~')                # Please, replace the path with your own.  
df = pd.read_csv('data_studentlist.csv',header='infer')
df.head(3)
df.groupby('gender').mean()
df.groupby('gender')['height'].mean()
df.groupby('gender')[['height','weight']].mean()
df.groupby('gender')[['grade','age']].std()
df.groupby('gender')['height'].describe()
sr = df.groupby(['gender','bloodtype'])['height'].mean()     # MultiIndexed result.
sr
sr.loc['F']
sr.loc['F'].loc['A']<jupyter_output><empty_output><jupyter_text>2.2.  DataFrame methods:<jupyter_code>df['height'].apply(lambda x: x/100)
df.sort_values(by='bloodtype')
df.sort_values(by='bloodtype', ascending=False)
df.sort_values(by=['bloodtype','gender'])
df['bloodtype'].unique()
df['bloodtype'].nunique()
# Frequency table.
df['bloodtype'].value_counts()
# Frequency table.
df['gender'].value_counts()<jupyter_output><empty_output><jupyter_text>2.3. Pivoting:<jupyter_code>my_dict = {"Size": ["L", "L", "M", "M", "M", "S", "S", "S", "S"],
           "Type": ["A", "A", "A", "B", "B", "A", "A", "B", "B"],
            "Location": ["L1", "L1", "L1", "L2", "L2", "L1", "L2", "L2", "L1"],
            "A": [1, 2, 2, 3, 3, 4, 5, 6, 7],
            "B": [2, 4, 5, 5, 6, 6, 8, 9, 9]}
df = pd.DataFrame(my_dict)
df
# Index by 'Size' and 'Type'. Columns by 'Location'. Values provided by the 'B' column.
dfr = pd.pivot_table(df, index=['Size','Type'], columns='Location', values='B')      
dfr
dfr.columns
dfr.index                                                                 # MultiIndex object!
# The same as above, but fill the missing values with 0.
pd.pivot_table(df, index=['Size','Type'], columns='Location', values='B', fill_value=0)
# The same as above with the aggregation function specified.
pd.pivot_table(df, index=['Size','Type'], columns='Location', values='B', aggfunc = np.mean, fill_value=0)
# Index by ''Location'. Columns by 'Size' and 'Type'. Values provided by the 'B' column.
dfr = pd.pivot_table(df, index='Location', columns=['Size','Type'], values='B')
dfr
dfr.index
dfr.columns                                                           # Now, MultiIndex object for the columns.
# The aggregation funciton is numpy.median().
pd.pivot_table(df, index=['Size','Type'], columns='Location', values='B', aggfunc = np.median, fill_value=0)
# Group averages of the columns 'A' abd 'B'.
pd.pivot_table(df, index=['Size','Type'], values=['A','B'], aggfunc=np.mean)
# Now, with groupby() method.
# The result is the same. 
df.groupby(['Size','Type'])[['A','B']].mean()
# Aggregate differenly the columns 'A' and 'B'.
pd.pivot_table(df, index=['Size','Type'], values=['A','B'], aggfunc={'A':np.max,'B':np.min})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20350109409190373, 'fraction_numerical': 0.01723194748358862, 'mean_word_length': 4.927066450567261, 'pattern_counts': {'":': 5, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '348443', 'license': 'MIT', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1336, 'n_words': 292, 'path': 'SIC_AI_Coding_Exercises/SIC_AI_Chapter_03_Coding_Exercises/ex_0208.ipynb', 'repo_name': 'AntonioFerreira1/SIC-Artificial-Intelligence', 'size': '3656'}