<jupyter_start><jupyter_text>![title](img/cover4.png) Copyright! This material is protected, please do not copy or distribute.    by:<NAME> ***Udemy course : Python Bootcamp for Data Science 2021 Numpy Pandas & Seaborn ***  The Exercises for  Module 12 Instruction:  Please write your code after the phrase "  Your codes begin here:"  Q1. Your first task is to read a dataframe from a csv file form the path: ('data/ex5.csv') with the name df1? Then display this dataframe:Hint1: To read a csv file you will need to use the function **pd.read_csv()**.Hint2: To display the head of a dataframe you will need to use the function head().<jupyter_code>import pandas as pd

## Your codes begin here:<jupyter_output><empty_output><jupyter_text>Q2. Your second task is to make this dataframe ('df1') a multi-indexed dataframe by setiing its index to two columns which are: ('year'  and	'quarters')? Then display the dataframe?**Hint1:** To set index you will need the function **set_index()**. and to pass the two columns in a list.**Hint2:** To save changes to the dataframe use the argument: **inplace = True**.<jupyter_code>## Your codes begin here:<jupyter_output><empty_output><jupyter_text>Q3. In this exercise, for the dataframe 'df1', you are required to move the 'quarters' from the index into the columns (which means to unstack this dataframe) and to save the modified dataframe to a new dataframe called 'df2'? Then display the new dataframe 'df2'?**Hint:** To unstack a dataframe you will need the function **unstack()**.<jupyter_code>## Your codes begin here:<jupyter_output><empty_output><jupyter_text>Q4. For the dataframe 'df2', reverse what you have done in Q3, i.e. move the quarters columns into the index again (moving columns to index is called stacking)? Then display the new dataframe 'df2'?**Hint:** To stack a dataframe you will need the function **stack()**.<jupyter_code>## Your codes begin here:<jupyter_output><empty_output><jupyter_text>Q5. In this dataframe named 'income' (which is created in the first code cell below): Make the categorical column with the label 'gender' two separate columns (This is called pivoting: transform one column into several columns)?**Hint1:** Do not forget to run the first cell below to create the dataframe first.**Hint2:** you will need the function **pivot()**. This function requires three arguments (**index**, **columns** and **values**).<jupyter_code># Just run this cell to create the dataframe 'income':


# Here you need to pivot the dataframe for the column 'gender'
## Your codes begin here:<jupyter_output><empty_output><jupyter_text>*** Solutions***  Q1. Your first task is to read a dataframe from a csv file form the path: ('data/ex5.csv') with the name df1? Then display this dataframe:<jupyter_code>import pandas as pd

## Your codes begin here:

df1 = pd.read_csv('data/ex5.csv')
df1<jupyter_output><empty_output><jupyter_text>Q2. Your second task is to make this dataframe ('df1') a multi-indexed dataframe by setiing its index to two columns which are: ('year'  and	'quarters')? Then display the dataframe?<jupyter_code>## Your codes begin here:

df1.set_index(['year', 'quarters'], inplace = True)
df1<jupyter_output><empty_output><jupyter_text>Q3. In this exercise, for the dataframe 'df1', you are required to move the 'quarters' from the index into the columns (which means to unstack this dataframe) and to save the modified dataframe to a new dataframe called 'df2'? Then display the new dataframe 'df2'?<jupyter_code>## Your codes begin here:

df2 = df1.unstack()
df2<jupyter_output><empty_output><jupyter_text>Q4. For the dataframe 'df2', reverse what you have done in Q3, i.e. move the quarters columns into the index again (moving columns to index is called stacking)? Then display the new dataframe 'df2'?<jupyter_code>## Your codes begin here:

df2.stack()<jupyter_output><empty_output><jupyter_text>Q5. In this dataframe named 'income' (which is created in the first code cell below): Make the categorical column with the label 'gender' two separate columns (This is called pivoting: transform one column into several columns)?<jupyter_code># Just run this cell to create the dataframe 'income':

income = pd.read_csv('data/ex6.csv')
income
# Here you need to pivot the dataframe for the column 'gender'
## Your codes begin here:

income.pivot(index = 'year', columns = 'gender', values = 'income')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09773755656108597, 'fraction_numerical': 0.011538461538461539, 'mean_word_length': 5.618263473053892, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '162941', 'license': 'MIT', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1289, 'n_words': 585, 'path': 'exercises/Exercise ch12.ipynb', 'repo_name': 'SiamakMushakhian/Numpy-Pandas-Seaborn', 'size': '4425'}