I can’t seem to update the style in a dataframe in Python

First, I don't know Python but am using it for a team project. I followed a plaid quickstart tutorial. I was able to create a dataframe with the data I got back, use it to create an html and generate a pdf from that. It took me HOURS! Now I want to apply some basic formatting to the data, like making amounts red if they are negative, right aligning the amount field. I have the code written, but I am not using Jupyter Lab, I am using atom. I have seen a reference to needing style.render() in order to get the changes but don't know how to do it.

This is my code which shows no changes in color in the pdf

# Execute query selecting Transactions table in Plaid database
    cur.execute("SELECT description, date, amount FROM transactions")

    # Fetch rows
    rows = cur.fetchall()

    # Close cursor

    # Close connection

    # set negative amounts to red
    def color_negative_red(amount):
        color = 'red' if amount < 0 else 'black'
        return 'color: %s' % color

    # dataframe
    df = pd.DataFrame (rows, columns = ["Transaction Description", "Date", "Amount"])

    # convert data frame to <table> and display
    html = df.to_html()

    text_file = open("test.html", "w")

    # output to a pdf
    import pdfkit
    pdfkit.from_file('test.html', 'out.pdf')

    # open report and pdf in a browser
    import webbrowser as wb
. ** Note this doesn't run because you need the data

pdf generated from test sandbox environment

Read more here: https://stackoverflow.com/questions/66323970/i-cant-seem-to-update-the-style-in-a-dataframe-in-python

Content Attribution

This content was originally published by Carole Tierney at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: