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.
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.
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.
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.
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:
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.
Reading a calculated column from a data module
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.
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:
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:
The following code 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:
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).
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: