<jupyter_start><jupyter_text>pandas Series  Setup<jupyter_code>import numpy as np
import pandas as pd

pd.set_option('display.max_columns', 10)
pd.set_option('display.max_rows', 10)<jupyter_output><empty_output><jupyter_text>Create Series from ndarray<jupyter_code>s1 = pd.Series(np.arange(0, 5))
s1<jupyter_output><empty_output><jupyter_text>Create Series with index<jupyter_code>s2 = pd.Series(np.arange(0, 5), index=['a', 'b', 'c', 'd', 'e'])
s2<jupyter_output><empty_output><jupyter_text>Assign index to Existing Series<jupyter_code>s2.index = ['A', 'B', 'C', 'D', 'E']
s2<jupyter_output><empty_output><jupyter_text>The index and values Properties<jupyter_code>s2.index
s2.values
type(s2.values)<jupyter_output><empty_output><jupyter_text>Create One-Item Series from Scalar<jupyter_code>s3 = pd.Series(5)
s3<jupyter_output><empty_output><jupyter_text>Create Series from list<jupyter_code>s4 = pd.Series([1, 2, 3, 4, 5])
s4<jupyter_output><empty_output><jupyter_text>Create Series from dict<jupyter_code>from datetime import date
bdays = {
    'John': date(1940, 10, 9),
    'Paul': date(1942, 6, 18),
    'George': date(1943, 2, 25),
    'Ringo': date(1940, 7, 7),
}
s5 = pd.Series(bdays)
s5<jupyter_output><empty_output><jupyter_text>An Array with Holes<jupyter_code>ar = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan, 9, 10])
ar
ar.mean()
s6 = pd.Series(ar)
s6
s6.mean(), sum([1, 2, 3, 5, 6, 7, 9, 10])/8
s6.mean(skipna=False)
s6.isna()
s6.notna()<jupyter_output><empty_output><jupyter_text>Check if `Series` contains an nan value<jupyter_code>s6.isna().values.any()<jupyter_output><empty_output><jupyter_text>loc[] and iloc[]<jupyter_code>rng = np.random.default_rng(seed=42)
s7 = pd.Series(rng.random(5), index=['a', 'b', 'c', 'd', 'e'])
s7
s7.loc['a'], s7.iloc[0]
s7.loc['b':'d']
s7.iloc[1:4]
indices = ['b', 'c', 'd']
s7.loc[indices]
s7.loc[['a', 'c', 'e']]
s7.iloc[[0, 2, 4]]<jupyter_output><empty_output><jupyter_text>Alignment<jupyter_code>grades1 = pd.Series([17, 44, 28, 8, 3], index=['A', 'B', 'C', 'D', 'F'])
grades2 = pd.Series([76, 122, 151, 21, 0], index=['D', 'C', 'B', 'A', 'F'])
grades1
grades2<jupyter_output><empty_output><jupyter_text>Adding `Series`<jupyter_code>grades_all = grades1 + grades2
grades_all
grades1 = pd.Series([17, 44, 28, 8, 3], index=['A', 'B', 'C', 'D', 'F'])
grades2 = pd.Series([76, 122, 151, 21], index=['D', 'C', 'B', 'A'])
grades_all = grades1 + grades2
grades_all<jupyter_output><empty_output><jupyter_text>`add()` with `fill_value`<jupyter_code>grades_all = grades1.add(grades2, fill_value=0)
grades_all<jupyter_output><empty_output><jupyter_text>Comparing Series<jupyter_code>mantle1968 = pd.Series([14, 1, 18, 54, 6], index=['2B', '3B', 'HR', 'RBI', 'SB'])
mantle1951 = pd.Series([11, 5, 13, 65, 8], index=['2B', '3B', 'HR', 'RBI', 'SB'])
mantle1968 > mantle1951
mantle1968[mantle1968 > mantle1951]<jupyter_output><empty_output><jupyter_text>Element-wise Operations<jupyter_code>rng = np.random.default_rng(seed=42)
exam_grades = pd.Series(rng.integers(60, 101, 100))
exam_grades
curved_grades = exam_grades.multiply(1.05)
curved_grades
def convert_to_letter(grade):
    if grade >= 90:
        return 'A'
    elif grade >= 80:
        return 'B'
    elif grade >= 70:
        return 'C'
    elif grade >= 65:
        return 'D'
    else:
        return 'F'
convert_to_letter(curved_grades)
letter_grades = curved_grades.apply(convert_to_letter)
letter_grades<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18691860465116278, 'fraction_numerical': 0.06366279069767442, 'mean_word_length': 6.838268792710706, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 71, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '519088', 'license': 'MIT', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1467, 'n_words': 271, 'path': 'pandas/Demos/series.ipynb', 'repo_name': 'WebucatorTraining/classfiles-actionable-python', 'size': '3440'}