Skip to content

Formatting

The module provides methods to perform formatting. It formats the columns of a given DataFrame and transforms the DataFrame to a list to be included in a Word document. See docxtpl.readthedocs for more information.

Accessor

Initialise the DataFrame with the formatting method. Minimal working example:

df.crm.formatting()

Returns:

Type Description
Formatting

Returns a class called "Formatting" providing formatting analytics methods.

Methods

df_to_tbl(label='')

Minimal working example:

df.crm.formatting().df_to_tbl()

Parameters:

Name Type Description Default
label str

Defines the top left entry of the resulting table.

''

Returns:

Type Description
list

Returns the DataFrame as a list to be used by the Python package "docxtpl" to create a table in a Word document.

format_cols(int_cols=None, float_cols=None, float_digits=1, pct_cols=None, pct_digits=1, date_cols=None, date_format=cfg.DATE_FORMAT, date_format_upper=False, shift_index=False)

Minimal working example:

df.crm.formatting().format_cols()

Parameters:

Name Type Description Default
int_cols Union[str, list]

Defines the columns to be formatted as integers, i.e. ",.0f".

None
float_cols Union[str, list]

Defines the columns to be formatted as floats, i.e. ",.<float_digits>f".

None
float_digits int

Defines the number of float digits.

1
pct_cols Union[str, list]

Defines the columns to be formatted as percentage, i.e. ",.<percentage_digits>%".

None
pct_digits int

Defines the number of percentage digits.

1
date_cols Union[str, list]

Defines the columns to be formatted as date, i.e., for example, "<%d.%m.%Y>".

None
date_format str

Defines the date format.

DATE_FORMAT
date_format_upper bool

Transforms the date to uppercase.

False
shift_index bool

Shifts the index by one.

False

Returns:

Type Description
DataFrame

Returns the formatted DataFrame.

Examples

data
>>> import credit_risk_modelling as crm
>>> data = crm.load_data.load_data()
>>> data

           DATE    ID GRADE  GRADE_PD OVERRIDE  OVERRIDE_PD  DEFAULT
0    2019-12-31    10     B    0.1000        B       0.1000        0
1    2019-12-31   100   BBB    0.0090       BB       0.0400        0
2    2019-12-31  1000   BBB    0.0090      BBB       0.0090        0
3    2019-12-31  1001   BBB    0.0090      BBB       0.0090        0
4    2019-12-31  1003   BBB    0.0090      BBB       0.0090        0
...         ...   ...   ...       ...      ...          ...      ...
4145 2023-12-31   994    AA    0.0010       AA       0.0010        0
4146 2023-12-31   995    AA    0.0010       AA       0.0010        0
4147 2023-12-31   996     A    0.0020        A       0.0020        0
4148 2023-12-31   998     B    0.1000        B       0.1000        0
4149 2023-12-31   999   AAA    0.0002      AAA       0.0002        0

[4150 rows x 7 columns]
.table()
>>> (
>>>     data
>>>     .crm.formatting()
>>>     .format_cols(
>>>         float_cols="OVERRIDE_PD", float_digits=2,
>>>         pct_cols="GRADE_PD", pct_digits=2,
>>>         date_cols="DATE", date_format="%d %b %Y", date_format_upper=True,
>>>         shift_index=True
>>>     )
>>> )

             DATE    ID GRADE GRADE_PD OVERRIDE OVERRIDE_PD  DEFAULT
1     31 DEC 2019    10     B   10.00%        B        0.10        0
2     31 DEC 2019   100   BBB    0.90%       BB        0.04        0
3     31 DEC 2019  1000   BBB    0.90%      BBB        0.01        0
4     31 DEC 2019  1001   BBB    0.90%      BBB        0.01        0
5     31 DEC 2019  1003   BBB    0.90%      BBB        0.01        0
...           ...   ...   ...      ...      ...         ...      ...
4146  31 DEC 2023   994    AA    0.10%       AA        0.00        0
4147  31 DEC 2023   995    AA    0.10%       AA        0.00        0
4148  31 DEC 2023   996     A    0.20%        A        0.00        0
4149  31 DEC 2023   998     B   10.00%        B        0.10        0
4150  31 DEC 2023   999   AAA    0.02%      AAA        0.00        0

[4150 rows x 7 columns]