<jupyter_start><jupyter_text>2) Creating Series Objects from Python List<jupyter_code>import pandas as pd
ice_cream = ['Chocolate', 'Vanilla', 'Grape', 'Lime']
pd.Series(ice_cream)
lottery = [4, 8, 15, 16, 23, 42]
pd.Series(lottery)
registrations = [True, False, False, True, False]
pd.Series(registrations)<jupyter_output><empty_output><jupyter_text>------  3) Create Series Object from Dictionary<jupyter_code>webster = {
    'Retriever': 'Cute Dog',
    'Apple': 'Yummy Fruit', 
    'Blue': 'Amazing Colour'
}

pd.Series(webster)<jupyter_output><empty_output><jupyter_text>-------  4) Intro to Attributes<jupyter_code>about_me = ['smart', 'charming', 'bralliant', 'humble']
s = pd.Series(about_me)
s
s.values
s.index
s.dtype<jupyter_output><empty_output><jupyter_text>------  6) Intro to Methods<jupyter_code>prices = [2.99, 3.1, 4.5]
s = pd.Series(prices)
s
s.sum()
s.product()
s.mean()<jupyter_output><empty_output><jupyter_text>-------  7) Parameters and Arguments<jupyter_code># Difficulty - Easy, Medium and Hard
# Volume - 1 through 10
# Subtitles - True or False
fruits = ['Apple', 'Orange', 'Plum', 'Grape', 'Blueberry']
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thrusday', 'Friday']

pd.Series(fruits, weekdays)
pd.Series(data=fruits, index=weekdays)
pd.Series(fruits, index=weekdays)<jupyter_output><empty_output><jupyter_text>Index values don't need to be unique in Pandas<jupyter_code>fruits = ['Apple', 'Orange', 'Plum', 'Grape', 'Blueberry', 'Watermelon']
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thrusday', 'Friday', 'Monday']

pd.Series(fruits, weekdays)<jupyter_output><empty_output><jupyter_text>----  8) Import `Series` with `read_csv` Method<jupyter_code>pd.read_csv('Data/pokemon.csv')<jupyter_output><empty_output><jupyter_text>**`squeeze=True` force the data frame into Series**<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
pokemon
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
google<jupyter_output><empty_output><jupyter_text>------  The `.head()` and `.tail()` Methods<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
pokemon.head(2)
google.tail(10)<jupyter_output><empty_output><jupyter_text>-----  11) Python Built In Functions<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
len(pokemon)
len(google)
type(pokemon)
dir(google) # all avaliable attributes and methods for that object
sorted(pokemon)
sorted(google)
list(pokemon)
dict(google)
max(pokemon), min(pokemon)
max(google), min(google)<jupyter_output><empty_output><jupyter_text>-----  12) More Series Attributes<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
pokemon.values
pokemon.index
pokemon.dtype
google.dtype
pokemon.is_unique # check whether every row values are unique or not
google.is_unique
pokemon.ndim # number of dimensions
google.ndim
pokemon.shape, google.shape
pokemon.size
google.size
pokemon.name # usually print out the top row name
google.name
pokemon.head()
# if we change the name and if we call .head()
pokemon.name = 'Poket Monster'

pokemon.head()<jupyter_output><empty_output><jupyter_text>-------  13) The `.sort_values()` Method<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
pokemon.sort_values().head()
pokemon.sort_values(ascending=False).head(10)
google.sort_values(ascending=False).head(1) #max stock value<jupyter_output><empty_output><jupyter_text>--------  14) The `inplace` Parameter<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
google.head(3)
google = google.sort_values()
google
google.sort_values(ascending=False, inplace=True)
google<jupyter_output><empty_output><jupyter_text>-----  15) The `.sort_index()` Method<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
pokemon.head()
pokemon.sort_values(ascending=False, inplace=True)
pokemon.head(3)
pokemon.sort_index()
pokemon = pokemon.sort_index(ascending=True)
pokemon.head()<jupyter_output><empty_output><jupyter_text>------  17) Python's `in` keyword<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
3 in [1, 3, 4, 5]
3 in [0, 1, 2, 5]
pokemon.head(3)
'Bulbasaur' in pokemon # by default pandas look at in Index position. Of course Bulbasaur is not in the index
# same as above code
'Bulbasaur' in pokemon.index
100 in pokemon.index
'Bulbasaur' in pokemon.values # now pandas can find Bulbasaur in values
'Pikachu' in pokemon.values<jupyter_output><empty_output><jupyter_text>------  18) Extract Values by Index Position<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
google = pd.read_csv('Data/google_stock_price.csv', usecols=['Stock Price'], squeeze=True)
pokemon.head(2)
pokemon[1]
pokemon[[100, 200, 300]]
pokemon[5: 10]
pokemon[: 10]
google[-1:]<jupyter_output><empty_output><jupyter_text>--------  19) Extract values by Index Label<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', index_col = ['Pokemon'], squeeze=True)
pokemon.head()
pokemon[0]
pokemon[:5]
pokemon[[100, 200, 300]]
pokemon['Pikachu']
pokemon['Electrode': 'Delcatty'] # when using index lable, both start and end will be included
pokemon[: 'Electrode': 2] # stepping 2 each time<jupyter_output><empty_output><jupyter_text>`reindex`<jupyter_code>pokemon.reindex(index = ['Pikachu', 'Digimon'])<jupyter_output><empty_output><jupyter_text>--------  21) The `.get()` Method on a `Series`<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', index_col = ['Pokemon'], squeeze=True)
pokemon.head(3)
pokemon = pokemon.sort_index()
pokemon.get(0)
pokemon.get('Pikachu')
pokemon.get([100, 200, 300])
pokemon.get(['Moltres', 'Meowth'])<jupyter_output><empty_output><jupyter_text>if we pass values or index which are not existed in Series, we won't get any results<jupyter_code>pokemon.get(1000)
pokemon.get('blah')
pokemon.get([0, 5, 1000])
pokemon.get(['blah', 'nonsense'])<jupyter_output><empty_output><jupyter_text>Instead what we can do is what we want to provide `by default` if there is no value avaliable for a specific keyIt is fall back option.<jupyter_code>pokemon.get(1000, default='This is not a Pokemon') # 1000 index is not avaliable, instead it will return default value
pokemon.get('blah', default='This is not a Pokemon')
pokemon.get('Pikachu', default='This is not a Pokemon') # default is a fall back option
pokemon.get(key=[0, 5], default='This is not a Pokemon')
pokemon.get(key=[0, 5, 1000], default='This is not a Pokemon')
pokemon.get(['Pikachu', 'Meowth'], default='This is not a Pokemon')
pokemon.get(['Blah', 'Nonsense', 'Pikachu'], default='This is not a Pokemon')<jupyter_output><empty_output><jupyter_text>---------  22) Math Methods on Series Object<jupyter_code>google = pd.read_csv('Data/google_stock_price.csv', squeeze=True)
google.head(3)<jupyter_output><empty_output><jupyter_text>`.count()` excludes NAN values<jupyter_code>google.count()
google.size<jupyter_output><empty_output><jupyter_text>`.len()` will include NAN values<jupyter_code>len(google)
google.sum()
google.mean()
google.sum()/len(google)
google.std()
google.min(), google.max()
min(google), max(google), sum(google)
google.median()
google.mode()
google.describe()<jupyter_output><empty_output><jupyter_text>--------  23) The `.idxmax()` and `.idxmin()` Methods<jupyter_code>google = pd.read_csv('Data/google_stock_price.csv', squeeze=True)
google.head(3)
google.min(), google.max()
google.idxmin(), google.idxmax() # returns the index of min and max values
google[11], google[3011] # we can then reterive the values by using those indexes
google[google.idxmin()]<jupyter_output><empty_output><jupyter_text>-------  24) The `.value_counts()` Method<jupyter_code>pokemon = pd.read_csv('Data/pokemon.csv', index_col=['Pokemon'], squeeze=True)
pokemon.head(3)
pokemon.value_counts()
pokemon.value_counts().sum()
pokemon.count()
pokemon.value_counts(ascending=True)<jupyter_output><empty_output><jupyter_text>---------  25) The `.apply()` Method<jupyter_code>google = pd.read_csv('Data/google_stock_price.csv', squeeze=True)
google.head(6)<jupyter_output><empty_output><jupyter_text>using Custom Function<jupyter_code>def classify_performance(number):
    if number < 300:
        return 'OK'
    elif number >= 300 and number < 650:
        return 'Satisfactory'
    else:
        return 'Incredible!'
google.apply(classify_performance)
google.apply(classify_performance).value_counts()<jupyter_output><empty_output><jupyter_text>using Lamdba function<jupyter_code>google.head(6)
# we want to prefix $ dollar sign
google.apply(lambda stock_price: '$'+str(stock_price))<jupyter_output><empty_output><jupyter_text>--------  26) The `.map()` Method<jupyter_code>pokemon_names = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
pokemon_names.head(3)
pokemon_types = pd.read_csv('Data/pokemon.csv', index_col='Pokemon', squeeze=True)
pokemon_types.head(3)<jupyter_output><empty_output><jupyter_text>`.map()` will look at the index of the second series and try to map accordingly and return the values of second series.<jupyter_code>pokemon_names.map(pokemon_types)<jupyter_output><empty_output><jupyter_text>In case of mistmatch index, return NAN<jupyter_code>pokemon_types.map(pokemon_names) # this return NAN because of mismatch index<jupyter_output><empty_output><jupyter_text>------  Mapping against Dictionary object<jupyter_code>pokemon_names = pd.read_csv('Data/pokemon.csv', usecols=['Pokemon'], squeeze=True)
pokemon_types = pd.read_csv('Data/pokemon.csv', index_col='Pokemon', squeeze=True).to_dict()
pokemon_names.head(3)
pokemon_types
pokemon_names.map(pokemon_types)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16016073478760046, 'fraction_numerical': 0.01588212782242633, 'mean_word_length': 7.230708661417323, 'pattern_counts': {'":': 0, '<': 135, '<?xml version=': 0, '>': 134, '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': '33', 'dir': 'jupyter-structured-clean-dedup', 'id': '528623', 'license': 'MIT', 'n_tokens_mistral': 4102, 'n_tokens_neox': 3891, 'n_words': 757, 'path': 'Pandas - Data Analysis with Pandas and Python - BP/02_Series.ipynb', 'repo_name': 'ptyadana/probability-and-statistics-for-business-and-data-science', 'size': '10452'}