Pandas create empty DataFrame with only column names

I have a dynamic DataFrame which works fine, but when there are no data to be added into the DataFrame I get an error. And therefore I need a solution to create an empty DataFrame with only the column names.

For now I have something like this:

    df = pd.DataFrame(columns=COLUMN_NAMES) # Note that there are now row data inserted.

PS: It is important that the column names would still appear in a DataFrame.

But when I use it like this I get something like that as a result:

    Index([], dtype='object')
    Empty DataFrame

The "Empty DataFrame" part is good! But instead of the Index thing I need to still display the columns.


An important thing that I found out: I am converting this DataFrame to a PDF using Jinja2, so therefore I'm calling out a method to first output it to HTML like that:


This is where the columns get lost I think.

Edit2: In general, I followed this example: The css is also from the link. That's what I do to send the dataframe to the PDF:

    env = Environment(loader=FileSystemLoader('.'))
    template = env.get_template("pdf_report_template.html")
    template_vars = {"my_dataframe": df.to_html()}

    html_out = template.render(template_vars)
    HTML(string=html_out).write_pdf("my_pdf.pdf", stylesheets=["pdf_report_style.css"])


If I print out the dataframe right after creation I get the followin:

    [0 rows x 9 columns]
    Empty DataFrame
    Columns: [column_a, column_b, column_c, column_d, 
    column_e, column_f, column_g, 
    column_h, column_i]
    Index: []

That seems reasonable, but if I print out the template_vars:

    'my_dataframe': '<table border="1" class="dataframe">\n  <tbody>\n    <tr>\n      <td>Index([], dtype=\'object\')</td>\n      <td>Empty DataFrame</td>\n    </tr>\n  </tbody>\n</table>'

And it seems that the columns are missing already.

E4: If I print out the following:


I get the following result already:

    <table border="1" class="dataframe">
          <td>Index([], dtype='object')</td>
          <td>Empty DataFrame</td>

You can create an empty DataFrame with either column names or an Index:

    In [4]: import pandas as pd
    In [5]: df = pd.DataFrame(columns=['A','B','C','D','E','F','G'])
    In [6]: df
    Empty DataFrame
    Columns: [A, B, C, D, E, F, G]
    Index: []


    In [7]: df = pd.DataFrame(index=range(1,10))
    In [8]: df
    Empty DataFrame
    Columns: []
    Index: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Edit: Even after your amendment with the .to_html, I can't reproduce. This:

    df = pd.DataFrame(columns=['A','B','C','D','E','F','G'])


    <table border="1" class="dataframe">
        <tr style="text-align: right;">


Back to homepage or read more recommendations: