<jupyter_start><jupyter_text>Chapter 6 - Working with CAS Tables<jupyter_code>import swat

conn = swat.CAS('server-name.mycompany.com', 5570, 'username', 'password')<jupyter_output><empty_output><jupyter_text>Using CASTable Objects like a DataFrame  CAS Table Introsspection Let's compare a Pandas DataFrame to a CASTable.<jupyter_code>import pandas as pd
df = pd.read_csv('/u/username/data/iris.csv')
df.columns<jupyter_output><empty_output><jupyter_text>Now we'll load a CAS table.<jupyter_code>tbl = conn.loadtable('data/iris.csv', caslib='casuser').casTable
tbl.columns<jupyter_output><empty_output><jupyter_text>Here are some side-by-side comparisons of methods of DataFrames and the same methods on CASTables.<jupyter_code>df.dtypes
tbl.dtypes
df.get_dtype_counts()
tbl.get_dtype_counts()<jupyter_output><empty_output><jupyter_text>Here are some other DataFrame methods being used on a CASTable object.<jupyter_code>tbl.size
tbl.shape
tbl.info()
tbl.head(n=3)
tbl.tail(n=4)
tbl.head(columns=['sepal_length', 'petal_length'])<jupyter_output><empty_output><jupyter_text>Computing Simple Statistics<jupyter_code>desc = df.describe()
desc
type(desc)
casdesc = tbl.describe()
casdesc
type(casdesc)
# conn.tableinfo('datasources.megacorp5m')
# %time mega.describe()<jupyter_output><empty_output><jupyter_text>Let's try the percentiles= option of the describe method.<jupyter_code>tbl.describe(percentiles=[0.3, 0.8])<jupyter_output><empty_output><jupyter_text>You can also filter the analyzed variables by type.<jupyter_code>tbl.describe(include='character')
tbl.describe(include='all')
tbl.describe(include=['numeric', 'character'])<jupyter_output><empty_output><jupyter_text>The types of output can also be selected.<jupyter_code>tbl.describe(stats=['count', 'nmiss', 'sum', 'probt', 'freq'])
tbl.describe(stats='all')<jupyter_output><empty_output><jupyter_text>Individual statistics can be run independently.<jupyter_code>tbl.count()
tbl.mean()
tbl.probt()<jupyter_output><empty_output><jupyter_text>Creating Plots from CASTable Data<jupyter_code>%matplotlib inline
# from matplotlib.pyplot import show
tbl.plot()
tbl[['sepal_length', 'sepal_width']].plot()
tbl[['sepal_length', 'sepal_width']].plot(kind='area')
tbl[['sepal_length', 'sepal_width']].plot.area()<jupyter_output><empty_output><jupyter_text>Exporting CASTables to Other Formats<jupyter_code>tbl.save(name='data/irisout.csv', caslib='casuser')
tbl.save(name='data/irisout.sashdat', caslib='casuser')<jupyter_output>ERROR: The SASHDAT file /u/username/data/irisout.sashdat already exists. Specify the replace option to overwrite an existing file.
ERROR: The action stopped due to errors.<jupyter_text>Sorting, Data Selection, and Iteration  Fetching Data with a Sort Order<jupyter_code>tbl.sort_values(['sepal_length', 'sepal_width'])
sorttbl = tbl.sort_values(['sepal_length', 'sepal_width'])
sorttbl
sorttbl.head(10)
sorttbl.tail(5)
sorttbl = tbl.sort_values(['sepal_length', 'sepal_width'], ascending=[False, True])
sorttbl
sorttbl.head(10)<jupyter_output><empty_output><jupyter_text>Iterating Through Columns and Rows  Basic Iteration<jupyter_code>for col in sorttbl:
    print(col)
for i, col in enumerate(sorttbl):
    print(i, col)
for col, dtype in zip(sorttbl, sorttbl.dtypes):
    print(col, dtype)<jupyter_output>sepal_length double
sepal_width double
petal_length double
petal_width double
species varchar<jupyter_text>Iterating Over Columns<jupyter_code>for col, obj in sorttbl.iteritems():
    print(col, obj)
    print('')
for row in sorttbl.iterrows():
    print(row)
for row in sorttbl.itertuples():
    print(row)<jupyter_output>(0, 7.9000000000000004, 3.7999999999999998, 6.4000000000000004, 2.0, 'virginica')
(1, 7.7000000000000002, 2.6000000000000001, 6.9000000000000004, 2.2999999999999998, 'virginica')
(2, 7.7000000000000002, 2.7999999999999998, 6.7000000000000002, 2.0, 'virginica')
(3, 7.7000000000000002, 3.0, 6.0999999999999996, 2.2999999999999998, 'virginica')
(4, 7.7000000000000002, 3.7999999999999998, 6.7000000000000002, 2.2000000000000002, 'virginica')
(5, 7.5999999999999996, 3.0, 6.5999999999999996, 2.1000000000000001, 'virginica')
(6, 7.4000000000000004, 2.7999999999999998, 6.0999999999999996, 1.8999999999999999, 'virginica')
(7, 7.2999999999999998, 2.8999999999999999, 6.2999999999999998, 1.8, 'virginica')
(8, 7.2000000000000002, 3.0, 5.7999999999999998, 1.6000000000000001, 'virginica')
(9, 7.2000000000000002, 3.2000000000000002, 6.0, 1.8, 'virginica')
(10, 7.2000000000000002, 3.6000000000000001, 6.0999999999999996, 2.5, 'virginica')
(11, 7.0999999999999996, 3.0, 5.9000000000000004, 2.100000000000000[...]<jupyter_text>Techniques for Indexing and Selecting Data  Selecting Columns by Label and Position<jupyter_code>col = sorttbl['sepal_width']
col
col.head()
sorttbl.species
widths = sorttbl[['sepal_width', 'petal_width', 'species']]
widths
widths.head()
widths.describe()
widths.columninfo()<jupyter_output><empty_output><jupyter_text>Selecting Data by Label and Position<jupyter_code>sorttbl
sorttbl.loc[:, 'petal_width'].head()
sorttbl.loc[:, 'sepal_length':'petal_length'].head()
sorttbl.loc[:, ['petal_width', 'sepal_width']].head()
sorttbl.loc[:, 3].head()
sorttbl.iloc[:, 0:3].head()
sorttbl.iloc[:, [3, 1]].head()
sorttbl.ix[:, [3, 'sepal_width']].head()
sorttbl.ix[:, 'sepal_width'].head()
sorttbl.ix[:, 'sepal_width':-2].head()
sorttbl.ix[:, ['sepal_width', 3, 4]].head()<jupyter_output><empty_output><jupyter_text>Dynamic Data Selection<jupyter_code>expr = sorttbl.petal_length > 6.5
expr.head()
newtbl = sorttbl[expr]
newtbl.head()
newtbl = sorttbl[sorttbl.petal_length > 6.5]
newtbl.head()
newtbl2 = newtbl[newtbl.petal_width < 2.2]
newtbl2.head()<jupyter_output><empty_output><jupyter_text>sorttbl[(sorttbl.petal_length > 6.5) &         (sorttbl.petal_width < 2.2)].head()<jupyter_code>sorttbl[(sorttbl.petal_length > 6.5) & 
        (sorttbl.petal_width < 2.2)]
sorttbl[(sorttbl.petal_length + sorttbl.petal_width) * 2 > 17.5].head()
sorttbl[sorttbl.species.str.upper().str.startswith('SET')].head()<jupyter_output><empty_output><jupyter_text>Data Wrangling on the Fly  Creating Computed Columns<jupyter_code>sorttbl['sepal_factor'] = ((sorttbl.sepal_length + sorttbl.sepal_width) * 2)
sorttbl.head()
sorttbl['total_factor'] = sorttbl.sepal_factor + sorttbl.petal_width + sorttbl.petal_length
sorttbl.head()
sorttbl['names'] = 'sepal / petal'
sorttbl.head()
sorttbl['cap_names'] = sorttbl.names.str.title()
sorttbl.head()<jupyter_output><empty_output><jupyter_text>By Groups Specifying groupings explicitly on the CASTable object.<jupyter_code>tbl.set_param('groupby', ['species'])
tbl
tbl.summary(subset=['min', 'max'])
tbl.del_param('groupby')
tbl<jupyter_output><empty_output><jupyter_text>Using the DataFrame method of specifying by groups.<jupyter_code>grptbl = tbl.groupby(['species'])
grptbl
grptbl.summary(subset=['min', 'max'])<jupyter_output><empty_output><jupyter_text>Concatenating By Groups<jupyter_code>grpsumm = grptbl.summary(subset=['min', 'max'])
grpsumm.concat_bygroups()<jupyter_output><empty_output><jupyter_text>Selecting Result Keys by Table Name<jupyter_code>grpcorr = grptbl.correlation()
grpcorr
grpcorr.get_tables('Correlation')
swat.concat(grpcorr.get_tables('Correlation'))<jupyter_output><empty_output><jupyter_text>Selecting a Specific By Group<jupyter_code>grpsumm.get_group(['versicolor'])
grpsumm.get_group(species='versicolor')<jupyter_output><empty_output><jupyter_text>Handling Multiple Sets of By Groups<jupyter_code>grpmdsumm = tbl.mdsummary(sets=[dict(groupby=['sepal_length']),
                                dict(groupby=['petal_length'])])
list(grpmdsumm.keys())
grpmdsumm.get_set(1)
grpmdsumm.get_set(1).concat_bygroups()<jupyter_output><empty_output><jupyter_text>Close the Connection<jupyter_code>conn.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14574536148432501, 'fraction_numerical': 0.09315419065898912, 'mean_word_length': 8.462469733656174, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 38, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '27', 'dir': 'jupyter-structured-clean-dedup', 'id': '615606', 'license': 'Apache-2.0', 'n_tokens_mistral': 3487, 'n_tokens_neox': 2913, 'n_words': 478, 'path': 'Chapter 6 - Working with CAS Tables.ipynb', 'repo_name': 'Suraj-617/sas-viya-python', 'size': '7815'}