Python notebook examples

Here are some examples that demonstrate how to work with Cognos Analytics data sources in a notebook using the Python programming language.

Tip: To switch your notebook's programming mode from R to Python, from the Kernel menu, select Change kernel > Python.

Reading a file

The following example reads a file called SampleFile_GOSales.xls, outputs the entire file, and then specifies the nrows and usecols parameters to output the City and Quantity columns for the first 2 rows of data in the file.

Notebook cells that read a file and then use the nrows and usecols parameters to display 2 rows of data with City and Quantity values.

Reading part of a file

Specify iterator and chunksize to work with chunks of data, rather than the whole data source all at once. The following example reads 20,000 rows at a time from the SampleFile_GOSales.xls file.

Notebook cells that read 20,000 rows at a time from the SampleFile_GOSales.xls file

Writing to a file

The following example writes the contents of a DataFrame table of data in to a file called regions sales, which is then stored in My content.

Notebook cells that write 3 rows of data to a file and stores it in My content

Reading a data module

A data module has relationships, aggregations, calculated columns, and so on, defined on its data. The read_data() method defines a section of data from a data module by selecting columns from the tables in the module. The data that is returned includes the relationships, aggregations, calculated columns, and so on, defined in the data module.

Specifying the read_data() method without the table_name parameter returns a DataFrame that contains the names of all the tables defined in the data module, as shown in the following example:

Notebook cells that read a data module and display its table names

In the following example, the M1_American_time_use_xlsx table in the sample data module data module is read and the columns Year and Children are returned.

Notebook cells that read M1_American_time_use_xlsx table in the sample data module data module and return Year and Children columns

Reading a calculated column from a data module Applies to version 11.1.5 and subsequent versions unless specifically overridden.

Use the calculation parameter to read a calculated column from a data module.

The following example reads 2 calculations from the calculation_data_module data module.

Notebook cells that read 2 calculations from the calculation_data_module data module

Reading a package

Like a data module, a package has relationships, aggregations, calculated columns, and so on, defined on its data. In addition, a package logically groups data into query subjects and folders. You can use the read_data() method to navigate through the structure of a package by using the query_subject and folder_name parameters.

The following code reads the Go data warehouse package:

Notebook cell that uses the read_data() method to read the GO data warehouse package

Specifying the folder_name parameter returns the contents of all folder(s) with that name in the package. If you want to return only the content of one specific folder, for example folder c, that's inside folder b, that's inside folder a, put a list of the folder names in the folder_name parameter. The following example uses the folder_name parameter to get all of the query subjects in the Employee expense folder:

Notebook cells that use the folder_name parameter to get all of the query subjects in the Employee expense folder

The following code returns the query items that are in the Employee expense fact query subject:

Notebook cells that returns the query items that are in the Employee expense fact query subject

The following code returns the query items that are in the Account query subject:

Notebook cells that return the query items that are in the Account query subject

The following code selects the first two query subjects in the Employee expense folder. Expense total is returned from the first query subject (Employee expense fact) and Account code is returned from the second query subject (Account).

Notebook cells that show how to use the query_subject parameter

Reading the metadata for OLAP data in a Framework Manager package

The following code shows the metadata parameter set to true, which returns the query subjects in the package:

Notebook cells that show how to use the metadata parameter for a OLAP data in a Framework Manager package