<jupyter_start><jupyter_text>NOTE: This assignment is adapted from: https://github.com/ajcr/100-pandas-puzzles/blob/master/100-pandas-puzzles.ipynb 1. Given a list of states = ['VA', 'MD', 'NY', 'NJ', 'TX'] and arbitrarary census data = list(range(10)), construct a MultiIndex object from the product of the two lists. Use it to index a ``Series`` of random numbers. Call this Series s.**Note you will have multiply indexed series with state name as the 1st level of index and census data (0 to 9) as the 2nd level index**<jupyter_code>states = ['VA', 'MD', 'NY', 'NJ', 'TX'] 
census_data = list(range(10))
print([(range(10))])
idx = pd.MultiIndex.from_product([states,census_data])
idx
s = pd.Series(np.random.rand(50), index=idx)
s.index.names = ['states', 'census_data']
s<jupyter_output><empty_output><jupyter_text>2. Check whether the index of s is lexicographically sorted. If not, apply sorting to ensure that you can use index slicing.<jupyter_code>df=s.sort_index()
df<jupyter_output><empty_output><jupyter_text>3. Select the labels 1, 3 and 6 from the second level of the MultiIndexed ``Series``.<jupyter_code>s.loc[:,[1,3,6]]<jupyter_output><empty_output><jupyter_text>4. Slice the ``DataFrame`` df; slice up to 4th state of the first level and from label 8 onwards for the second level.<jupyter_code>df1.loc[slice(None, 'TX'), slice(8, None)]<jupyter_output><empty_output><jupyter_text>5. Sum the values in s for each label in the first level (you should have Series giving you a total for labels 'VA', 'MD', 'NY', 'NJ' and 'TX').<jupyter_code>s.sum(level=0)<jupyter_output><empty_output><jupyter_text>6. Using unstack, create a ``DataFrame`` from s. States index will become new columns.<jupyter_code>df=s.unstack()
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13458262350936967, 'fraction_numerical': 0.019875070982396367, 'mean_word_length': 6.221311475409836, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '113364', 'license': 'Unlicense', 'n_tokens_mistral': 635, 'n_tokens_neox': 607, 'n_words': 195, 'path': 'Data 690 Lecture 12 HW_SaidaBabu_pandas series.ipynb', 'repo_name': 'iamsaida/python-codes', 'size': '1761'}