<jupyter_start><jupyter_text>Having learned the fundamentals of working with DataFrames, you will now move on to more advanced indexing techniques. You will learn about MultiIndexes, or hierarchical indexes, and learn how to interact with and extract data from them.  Changing index of a DataFrameAs you saw in the previous exercise, indexes are immutable objects. This means that if you want to change or modify the index in a DataFrame, then you need to change the whole index. You will do this now, using a list comprehension to create the new index.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.read_csv('sales/sales.csv', index_col = 'month')<jupyter_output><empty_output><jupyter_text>Before getting started, print the sales DataFrame in the IPython Shell and verify that the index is given by month abbreviations containing lowercase characters.<jupyter_code>sales
sales.index

# Create the list of new indexes: new_idx
new_idx = [month.upper() for month in sales.index]

# Assign new_idx to sales.index
sales.index = new_idx

# Print the sales DataFrame
sales<jupyter_output><empty_output><jupyter_text>Changing index name labelsNotice that in the previous exercise, the index was not labeled with a name. In this exercise, you will set its name to 'MONTHS'.  Similarly, if all the columns are related in some way, you can provide a label for the set of columns.To get started, print the sales DataFrame in the IPython Shell and verify that the index has no name, only its data (the month names).<jupyter_code>from IPython import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
# Assign the string 'MONTHS' to sales.index.name
sales.index.name = 'MONTHS'

# Print the sales DataFrame
sales

# Assign the string 'PRODUCTS' to sales.columns.name 
sales.columns.name = 'PRODUCTS'

# Print the sales dataframe again
sales<jupyter_output><empty_output><jupyter_text>Building an index, then a DataFrameYou can also build the DataFrame and index independently, and then put them together. If you take this route, be careful, as any mistakes in generating the DataFrame or the index can cause the data and the index to be aligned incorrectly.In this exercise, the sales DataFrame has been provided for you without the month index. Your job is to build this index separately and then assign it to the sales DataFrame. Before getting started, print the sales DataFrame in the IPython Shell and note that it's missing the month information.<jupyter_code>sales_1 = pd.read_csv('sales/sales.csv')
sales_1
sales_1 = sales_1.iloc[:,1:]
sales_1
# Generate the list of months: months
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']

# Assign months to sales_1.index
sales_1.index = months

# Print the modified sales_1 DataFram
sales_1<jupyter_output><empty_output><jupyter_text>Multiindexing<jupyter_code>sales_2 = sales_1.reset_index().drop('index', axis = 1)
sales_2
sales_2['state'] = ['CA', 'CA', 'NY', 'NY', 'TX', 'TX']
sales_2['month'] = [1, 2, 1, 2, 1, 2]
sales_2
# Set the index to be the columns ['state', 'month']: sales
sales_2 = sales_2.set_index(['state', 'month'])

# Sort the MultiIndex: sales
sales_2 = sales_2.sort_index()

# Print the sales DataFrame
sales_2
# Access the data from 'NY' and print it to verify that you obtain two rows.
sales_2.loc[['NY']]<jupyter_output><empty_output><jupyter_text>Indexing multiple levels of a MultiIndexLooking up indexed data is fast and efficient. And you have already seen that lookups based on the outermost level of a MultiIndex work just like lookups on DataFrames that have a single-level Index.Looking up data based on inner levels of a MultiIndex can be a bit trickier. In this exercise, you will use your sales DataFrame to do some increasingly complex lookups.The trickiest of all these lookups are when you want to access some inner levels of the index. In this case, you need to use slice(None) in the slicing parameter for the outermost dimension(s) instead of the usual :, or use pd.IndexSlice.<jupyter_code># Look up data for NY in month 1: NY_month1
NY_month1 = sales_2.loc[('NY', 1), :]
NY_month1

# Look up data for CA and TX in month 2: CA_TX_month2
CA_TX_month2 = sales_2.loc[(['CA', 'TX'], 2), :]
CA_TX_month2
# Look up data for all states in month 2: all_month2
all_month2 = sales_2.loc[(slice(None), 2), :]
all_month2
N<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07517084282460136, 'fraction_numerical': 0.01070615034168565, 'mean_word_length': 5.281831187410587, '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': '69483', 'license': 'MIT', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1288, 'n_words': 589, 'path': 'Skill Tracks/Data Manipulation with Python/Course 2 - Manipulating DataFrames with pandas/Chapter 2 -  Advanced indexing.ipynb', 'repo_name': 'indmitDS/DataCamp-DataScience', 'size': '4390'}