<jupyter_start><jupyter_text>Python NRT (Not a Real Tutorial)A brief brief tour aroundPython 2.7 & Pandas library  Python language * Good language to start programming with* Simple, powerful, mature* Easy to read, intuitive ```>>> print "how "+"are you?"how are you?```  Running Python * From Python console ```$ pythonPython 2.7.12 (default, Jun 29 2016, 14:05:02)[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwinType "help", "copyright", "credits" or "license" for more information.>>>``` * Run a python script ```$ python myprogram.py``` * Use an interactive web console like Jupyter ```$ jupyter notebook```  Syntax * No termination character<jupyter_code>name = "Pepe"<jupyter_output><empty_output><jupyter_text>* Blocks specified by indentation (not braces nor brackets)* First statement of a block ends in colon (:)<jupyter_code>def myfunction(x):
    pass
    if x > 10:
        pass
        pass
        return "bigger"
    else:
        pass
        pass
        return "smaller"
    
print "This number is: " + myfunction(5)<jupyter_output>This number is: smaller<jupyter_text>* Comments use number/hash symbol () and triple quotes (""")<jupyter_code>"""This is a comment that spands for
more than one line"""
# This is a one line comment
print "This line is executed"<jupyter_output>This line is executed<jupyter_text>Modules<jupyter_code>import pandas as pd
from time import clock<jupyter_output><empty_output><jupyter_text>Lists and selections<jupyter_code>months = ["Jan", "Feb", 3, 4, "May", "Jun"]
print months[0]
print months[1:3]  # slice operator :
print months[-2:]<jupyter_output>['May', 'Jun']<jupyter_text>Tuples Similar to lists, sequence of elements that conforms an immutable object.<jupyter_code>tup = ('physics', 'chemistry', 1997, 2000)
print tup[0]
print tup[1:3]<jupyter_output>('chemistry', 1997)<jupyter_text>Functions & Methods<jupyter_code>"""functions are pieces of code that you can 
call/execute, they are defined with the def keyword"""

def hola_mundo():
    print "Hola Mundo!"
""" methods are attributes of an object that 
you can call over the object with and "." """

s = "How are you" 
print s.split(" ")<jupyter_output>['How', 'are', 'you']<jupyter_text>Control flow * Loops (while, for)<jupyter_code>for numbers in range(1,5):
    print numbers<jupyter_output>1
2
3
4<jupyter_text>* Conditionals (if, elif, else)<jupyter_code>united_kingdom = ["England", "Scotland", "Wales", "N Ireland"]
one = "France"

if one in united_kingdom:
    print "UK"
elif one == "France":
    print "Not UK. Bon jour!"
else:
    print "Not UK"<jupyter_output>Not UK. Bon jour!<jupyter_text>Help! "house".len()?len(house)?<jupyter_code>help(len)
len("house")
help(list)<jupyter_output>Help on class list in module __builtin__:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |  
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 |  
 |  __delslice__(...)
 |      x.__delslice__(i, j) <==> del x[i:j]
 |      
 |      Use of negative indices is not supported.
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __getslice__(...)
 |      x.__getslice__(i, j) <==> x[i:j]
 |      
 |      Use of negative indices is not supported.
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __iadd__(...)
 |      x.__iadd__(y) <==> x+=y
 |  
 |  __imul__(...)
 |  [...]<jupyter_text>PANDAS LIBRARY Open source library  providing high-performance **structures** and **data analysis tools** for the Python programming language.  Import<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Structures  Pandas Series<jupyter_code>ss = pd.Series([1,2,3], 
              index = ['a','b','c'])
ss<jupyter_output><empty_output><jupyter_text>Selection<jupyter_code>ss = pd.Series([1,2,3], 
              index = ['a','b','c'])
print ss[0]       # as a list
print ss.iloc[0]  # by position, integer
print ss.loc['a'] # by label of the index
print ss.ix['a']  # label (priority)
print ss.ix[0]    # position if no label
"""Be careful with the slice operator 
using positions or labels"""

print ss.iloc[0:2] # positions 0,1
print ss.loc['a':'c'] # labels 'a','b','c'<jupyter_output>a    1
b    2
dtype: int64
a    1
b    2
c    3
dtype: int64<jupyter_text>Built-in methods<jupyter_code>pd.Series([1, 2, 3]).mean()
pd.Series([1, 2, 3]).sum()
pd.Series([1, 2, 3]).std()<jupyter_output><empty_output><jupyter_text>Pandas Dataframe<jupyter_code>df = pd.DataFrame(
    data =[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    index=['row1', 'row2', 'row3'],
    columns=['col1', 'col2', 'col3'])
df<jupyter_output><empty_output><jupyter_text>Selection Select columns<jupyter_code>df['col1']  # one col => Series
df[['col1']] # list of cols => DataFrame<jupyter_output><empty_output><jupyter_text>Select rows<jupyter_code>df.loc['row1'] # by row using label
df.iloc[0]  # by row using position
df.ix['row1']   # by row, using label
print df.ix[0]  # by row, using position<jupyter_output>col1    1
col2    2
col3    3
Name: row1, dtype: int64<jupyter_text>Combined selection<jupyter_code>print df.loc['row1',['col1', 'col3']] # labels
print df.loc[['row1','row3'],'col1' : 'col3']
df.iloc[0:2,[0,2]]  # row position 0,1
print df.ix[0,['col2','col3']] # position & label
print df.ix['row1':'row3', :]<jupyter_output>col2    2
col3    3
Name: row1, dtype: int64
      col1  col2  col3
row1     1     2     3
row2     4     5     6
row3     7     8     9<jupyter_text>Should I use always .ix()? .ix() selector gotcha!<jupyter_code>df2 = pd.DataFrame(
    data =[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    index=[1, 2, 3],
    columns=['col1', 'col2', 'col3'])

print df2.ix[1] # priority is label
# df2.ix[0]  ERROR!!
df2 = pd.DataFrame(
    data =[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    index=[1, 2, 3],
    columns=['col1', 'col2', 'col3'])

print df2.ix[1:3] # LABELS!! (1,2,3)
# these two dataframes are the same!!
df2 = pd.DataFrame(
    data =[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    index=[1, 2, 3],
    columns=[1, 2, 3])

df3 = pd.DataFrame(
    data =[[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df3<jupyter_output><empty_output><jupyter_text>DataFrame Selection Summary<jupyter_code>df['col1']     # by columns
df.loc['row1'] # by row, using label
df.iloc[0]     # by row, using position
df.ix['row2']  # by row, using label
df.ix[1]       # by row, using position<jupyter_output><empty_output><jupyter_text>Built-in method<jupyter_code>df.mean() # operates by columns (axis=0)<jupyter_output><empty_output><jupyter_text>Pandas Axis | axis   |     axis       |  along            |  each          ||--------|:--------------:|------------------:|---------------:|| axis=1 | axis="columns" | along the columns |for each row    || axis=0 | axis="index"   | along the rows    |for each column |<jupyter_code>df2 = pd.DataFrame(
    data =[[1, 2], [4, 5], [7, 8]],
    columns=["A", "B"])
df2
df2.mean(axis=1) # mean for each row
df2 = pd.DataFrame(
    data =[[1, 2], [4, 5], [7, 8]],
    columns=["A", "B"])
df2
df2.drop("A", axis=1) # drop columns for each row<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17724583109198494, 'fraction_numerical': 0.03469607315761162, 'mean_word_length': 3.746011486917677, 'pattern_counts': {'":': 1, '<': 89, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '504289', 'license': 'MIT', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2819, 'n_words': 737, 'path': 'Python_NRT.ipynb', 'repo_name': 'PyLadiesDC/python-for-journalists', 'size': '7436'}