简体   繁体   中英

How to best generate text from analysis done in Jupyter notebooks

I have done some analysis within a Jupyter notebook. It's quite a lot (tables etc.) and now I want to generate some text to highlight specific areas. The context is financial analysis.

So here an example I want to generate:

In <March 2022> most money (<AMOUNT>) was spent on <Category Level 0>, which is <AMOUNT 1> more/less than in the previous month and <AMOUNT 2> more/less than in the previous year.

Currently I am thinking to use:

from IPython.core.display import display, HTML
display(HTML('<h1>Analysis for</h1>'))

But this will be quite ugly as I need to stitch everything together. Is there a more elegant way to do this.

Thanks a lot for your directions

Why not write a function that returns a formatted string? You could reuse this easily.

def formatData(date, amount, categoryLevel, amount1, amount2):
    return "In {} most money {} was spent on {}, which is {} more/less than in the previous month and {} more/less than in the previous year.".format(date, amount, categoryLevel, amount1, amount2)

Then call it on each datum as follows

from datetime import date
formatData(date.today(), 2000, "Category Level 1", 200, 100)

which yields to

'In 2022-02-22 most money 2000 was spent on Category Level 1, which is 200 more/less than in the previous month and 100 more/less than in the previous year.'

This obviously works with all data types. You could then generate HTML or Markdown in order to create a report file in the desired format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM