import pandas as pd
import swat

"""
Explanation: Jupyter notebook
This Jupyter notebook provides the commands used in the article Great tip for dynamic data selection using SAS Viya and Python.
Import python packages
End of explanation
"""


conn = swat.CAS('sasserver.demo.sas.com',8777,'sasdemo','Orion123')

"""
Explanation: Connect to the CAS server
End of explanation
"""


conn

"""
Explanation: Check connection
End of explanation
"""


df=conn.upload('https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv')

"""
Explanation: Upload data into a DataFrame
End of explanation
"""


tbl = df.casTable

"""
Explanation: Load data into a CAS table
End of explanation
"""


sorttbl = tbl.sort_values(['sepal_length','sepal_width'],
                         ascending=[False,True])

"""
Explanation: Sort the stored values in a variable and change the sort order
End of explanation
"""


expr = sorttbl.petal_length > 6.5

newtbl = sorttbl[expr]

newtbl.head()

"""
Explanation: Expression to create filter and display data
End of explanation
"""


newtbl = sorttbl[sorttbl.petal_length > 6.5]

newtbl.head()

"""
Explanation: Same expression as above writte on one line
End of explanation
"""


newtbl2 = newtbl[newtbl.petal_width < 2.2]

newtbl2.head()

"""
Explanation: Another expression to change the filter criteria
End of explanation
"""


sorttbl[(sorttbl.petal_length > 6.5) &
        (sorttbl.petal_width < 2.2)].head()

"""
Explanation: Expression to add a bitwise comparison (&) to join the filters
End of explanation
"""


sorttbl[(sorttbl.petal_length > 6.5) & 
        (sorttbl.petal_width < 2.2)]

"""
Explanation: Display the CASTable object
End of explanation
"""


sorttbl[(sorttbl.petal_length + sorttbl.petal_width)
        * 2 > 17.5].head()

"""
Explanation: Expression that includes mathematical operations
End of explanation
"""


sorttbl[sorttbl.species.str.upper().str.startswith('SET')
        ].head()

"""
Explanation: Expression that uses the Python str method to make comparisions on character columns
End of explanation
"""


