<jupyter_start><jupyter_text>pandas seriesone dimensional labeled array - a more powerful version of a Python List<jupyter_code>import pandas as pd
# determine version of pandas
pd.__version__
# first, create a simple python List of strings 
icecream = ['chocolate','vanilla','strawberry','coffee'] 
type(icecream)
# create a pandas series from the python list using the pandas Series constructor method
pd.Series(icecream)    # pandas Series constructor method 
# a 'dtype: object' is the pandas internal notation for a string 

# one of the big advantages of a pandas.Series over a python list is that the index does not have to be 
#    numeric. A numeric index starting with 0 is the default. More on that later. 

# ?? a pandas series is immutable
# ?? a pandas list is immutable
# create a simple pandas list
lottery = [4,8,15,16,23,42]
lottery
type(lottery)
# from the python List construct a pandas Series using the Series constructor method  
lottery = pd.Series(lottery)
# Series() is a pandas constructor method - that means that it takes a python list and constructs a pandas series obect

type(lottery)
regs1 = [True, True, False, False, False]
type(regs1)
regs1
# create a pandas Series from the python list of booleans 
regs2 = pd.Series(regs1)
type(regs2)
regs2<jupyter_output><empty_output><jupyter_text>non numeric index<jupyter_code># create a python dictionary
webster = { "Aardvark" : "an animal",
            "Banana" : "a delicious fruit",
           "Cyan" : "a color"
}
type(webster)

webster
pd.Series(webster)
type(pd.Series(webster))<jupyter_output><empty_output><jupyter_text>pandas attributes and methods<jupyter_code>about_me = ['smart','handsome','charming','brilliant','humble']
about_me
type(about_me)
s = pd.Series(about_me)
s 
type(s)<jupyter_output><empty_output><jupyter_text>first, attributes attributes do nut use parenthesis<jupyter_code># access Series s attributes 
s.values     # the values attribute of the s series - values return .. values
s.dtype    # dtype = O which means an object (or string) 
# the dtype attribute returns the data type property
s.index    # describes the index of the pandas.Series<jupyter_output><empty_output><jupyter_text>now methods<jupyter_code>prices = [2.99, 4.45, 1.36]    # create a python List
type(prices)
prices    # a python list
# transform into pandas Series
s = pd.Series(prices)
s
s.sum()
s.mean()    # 
s.prod()    # multiplies all values<jupyter_output><empty_output><jupyter_text>more methods - parameters and arguments1. Difficulty - Easy, Medium, Hard2. Volume - 1 through 103. Subtitles - Tue / False<jupyter_code># create python Lists
fruits = ['apple','orange','plum','grape','blueberry']
weekdays = ['mon','tue','wed','thu','fri']



# create pandas Series from the python Lists
pd.Series(fruits, weekdays)
pd.Series(data = fruits, index = weekdays)     # explicit - identical to above line 

type(fruits), type(weekdays)
fruits.append('watermellon')
fruits
weekdays.append('mon')
weekdays
k = pd.Series(fruits, weekdays)    
# note: unlike a python List, pandas Series index labels do not need to be unique 
# pandas.Series are more robust python.list meets python.dict
k
type(k)
k.dtypes
k.shape<jupyter_output><empty_output><jupyter_text>read_csv methodhead and tail<jupyter_code>p = pd.read_csv("data/pokemon.csv")    # 
p.head()
# this is the syntax for importing only the Pokeman column
p = pd.read_csv('data/pokemon.csv', usecols = ['Pokemon'])
p.head()
# note the type is automatically (by default in pandas) set to a pandas.DataFrame
#   - Even if only one column  
type(p)
p.dtype
# so to import as a python Series object, use the 'squeeze=True' parameter
p = pd.read_csv('data/pokemon.csv', usecols = ['Pokemon'], squeeze = True)
p.head()
type(p)    # note that we now have a pandas.Series (no longer the default dataframe)
p.head()
# new read_csv object import as Series
g = pd.read_csv('data/google_stock_price.csv', squeeze = True)
type(g)
g.head()<jupyter_output><empty_output><jupyter_text>methods: head and tail<jupyter_code># from previous notebook 
p = pd.read_csv('data/pokemon.csv', usecols = ['Pokemon'], squeeze = True)
g = pd.read_csv('data/google_stock_price.csv', squeeze = True)

print(len(p))
print(len(g)) 
p.head(12) 
g.tail(20)
len(p)
len(g)
type(p)
type(g)
dir(p)    # all available methods including hidden methods (the ones with underscores) 
sorted(p)
type(p)
p.head(10)
list(p)    # turn pandas.Series into a python list 
type(list(p))

# duh!
# or a pythonb dict
dict(p)
max(g)
min(g)
p.sort_values().to_frame().head(10)
p
g
type(p)
p.values    # values is a panda.Series attribute 
g.values    # values is a panda.Series attribute 
g.index
g.dtype
p.dtype
p.is_unique    # the pokemon series are all unque names
g.is_unique    # values in google Series are not unique - some trading days closed as same price
p.shape
g.shape
p.size
g.size
p.name
g.name
g.name = 'sprice'
g.name<jupyter_output><empty_output><jupyter_text>sort() method<jupyter_code>p.head()
p.sort_values()
p.sort_values().head(10)
p.sort_index().head(10)
p.sort_values(ascending=False).tail(10)   # changes sort to descending
g
g.sort_values(ascending = True).head()
g
g.describe()
g = g.sort_values(ascending=True).head()    # this re assigns a new series to the variable
# note that now the variable g has a new series
g.describe()
# compare this output to previous g.describe() line<jupyter_output><empty_output><jupyter_text>pandas in  extract values by index  extract by get() method  blah<jupyter_code># create a python list 
[1,2,3,5,8]

8 in [1,2,3,5,8]
100 in [1,2,3,5,8]
p.head()
"Charmander" in p
3 in p
# identical to 
3 in p.index
p.values
"Charmander" in p.values
# find a value by index notation

p[3]
type(p[3])
# find multiple values by index 
p[[0,1,2,3,50]]
type(p[[0,1,2,3,50]])
p.head()    # same as p[:5]
p[:5]    # same as p.head() 
p[-5:]    # same as p.tail()
p.tail()    # same as p[:-5]
# so how to create a series with the pokemon name as the index .. 
# the base import looks like this DataFrame 
pd.read_csv('data/pokemon.csv')

# specify pokeman name as index instead of the default 0-720 integers
p = pd.read_csv('data/pokemon.csv', index_col='Pokemon', squeeze=True)    
# correct row/column notation ['colname']
# col can also be abreviated as index_col='Pokemon' 
p
# the zebra row html table formatting in Jupyter indicates that it is a dataframe


# notice that the number index still works
p[-1:]
p[3]
p.index[3]
type(p)
# and
p["Charmander"]
type(p['Charmander'])
p[['Charmander','Avalugg']]
p.head(3)
# using the get() method

p.head(10)
print(p.sort_index().head(10)) # sorted, but not permanent
print(p.head(10))
# but with inplace=True ... 
p.sort_index(inplace=True)
print(p.head(10))    # now, as a result of inplace=True, the base df is sorted<jupyter_output><empty_output><jupyter_text>use get()<jupyter_code>p.get('Absol')
print(type(p.get(['Absol','Moltres','Meowth'])))
p.get(['Absol','Moltres','Meowth'])    # shortcut for p.get(key = ['Absol','Moltres','Meowth'])
# 
p.get(key ='Meowth')
p.get(key = 'shit stain')    # unknown key processes successfully, but returns nothing 

p.get(key = 'shit stain', default='key not recognized') 
p['Bulbasaur']
p['shit stain']    # returns error
p.get('shit stain')    # valid command, unknown index value so returns null
p.get(key = 'shit stain')    # valid command (more explicit), but still unknown index and still returns null
p.get(key = 'shit stain', default = 'not a valid key')
    # valid, explicit, unknown index, instead of null returns specified default value
p.get(key = ["Charizard","poop stain"])
p.get(key = ["Charizard","poop stain"])
p.get(key = ["skid mark","poop stain"])
p.get(key = ["skid mark","poop stain"], default = 'bad values')<jupyter_output><empty_output><jupyter_text>math methods on series<jupyter_code>g.count()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12625556261919899, 'fraction_numerical': 0.012968849332485696, 'mean_word_length': 4.90984222389181, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '57966', 'license': 'MIT', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2735, 'n_words': 851, 'path': 'UdemyPandas/2017-07-notebooks/05 pandas series.ipynb', 'repo_name': 'simonbaier/learn-pandas', 'size': '7865'}