<jupyter_start><jupyter_text>The MultiIndex Object  View as Copy<jupyter_code>import pandas as pd
mlo = pd.read_csv('./Pandas Dataset - I/co2-mm-mlo.csv', na_values=-90.99, index_col='Date', parse_dates=True)
mlo.head()
s = mlo["Interpolated"]
s
mlo.assign(smooth=s.rolling(12).mean()).tail()<jupyter_output><empty_output><jupyter_text>a copy is returned<jupyter_code>mlo.head()
s2 = mlo.loc[:'1958-05', 'Average']
s2<jupyter_output><empty_output><jupyter_text>A view is returned<jupyter_code>s2[:] = 313
s2
mlo.head()<jupyter_output><empty_output><jupyter_text>Chained Indexing<jupyter_code>mlo['Average']['1958-03']
mlo['Average']['1958-03'] = 312<jupyter_output><ipython-input-16-4325b1ea7d19>:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  mlo['Average']['1958-03'] = 312
C:\Users\hasee\anaconda3\lib\site-packages\pandas\core\indexing.py:1637: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_single_block(indexer, value, name)<jupyter_text>Generally speaking, chained indexing is not a good practice. To set a new value, use `mlo.loc[row_indexer, col_indexer]` because `mlo.loc` is guaranteed to be `mlo` itself.<jupyter_code>mlo.loc['1958-03', 'Average']<jupyter_output><empty_output><jupyter_text>Hierarchical indexing<jupyter_code>h_index = pd.MultiIndex.from_product([['first', 'second'], ['A', 'B']])
h_index
x = pd.Series(range(4), index=h_index)
x
x['first']
x['first']['B']<jupyter_output><empty_output><jupyter_text>In the above, there are two selection operations.<jupyter_code>x.loc[('first', 'B')]<jupyter_output><empty_output><jupyter_text>In the above, there is a single selection operation. We can end up with a hierarchical index when stacking records.<jupyter_code>gl = pd.read_csv('./Pandas Dataset - I/co2-mm-gl.csv', na_values=-99.99, index_col='Date', parse_dates=True)

gl = gl[['Average']]
gl.columns = ['Average_gl']
gl.head()
ml = mlo[['Average']]
ml.columns = ['Average_mlo']
ml.head()
ml = ml[ml.index >= '1980-01']

gl = gl.head()
ml = ml.head()
gl
ml
multi = pd.concat([ml, gl], axis=1).stack()
multi
multi.index
multi.index.get_level_values('Date')
multi.loc[multi.index.get_level_values('Date') < '1980-03']<jupyter_output><empty_output><jupyter_text>Reshaping The `stack()` function compressed a level in the DataFrame’s columns to produce a Series (as a reminder, `multi = pd.concat([ml, gl], axis=1).stack()`).<jupyter_code>pd.concat([ml, gl], axis=1)
multi
multi.unstack()<jupyter_output><empty_output><jupyter_text>Pivoting<jupyter_code>rec = pd.concat([ml, gl], axis=1).stack().reset_index()
rec.columns = ['date', 'variable', 'value']
rec<jupyter_output><empty_output><jupyter_text>The above data is in 'stacked' or 'record' format.<jupyter_code>rec
rec[rec.variable == 'Average_mlo']
pivot_table = rec.pivot(index='date', columns='variable', values='value')
pivot_table<jupyter_output><empty_output><jupyter_text>The pivoted data is more suitable for timeseries analysis.<jupyter_code>pivot_table['Average_gl']
pivot_table['Average_mlo']
pivot_table.index<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1568627450980392, 'fraction_numerical': 0.02575358501609599, 'mean_word_length': 7.46039603960396, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 2, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '261945', 'license': 'MIT', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1286, 'n_words': 247, 'path': 'Day_39(Pandas).ipynb', 'repo_name': 'HaseebRajput007/100DaysOfCode-DataScience', 'size': '3417'}