Python Pandas 创建只有列名的空 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44513738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas create empty DataFrame with only column names
提问by E. Muuli
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.
我有一个可以正常工作的动态 DataFrame,但是当没有要添加到 DataFrame 中的数据时,我收到错误消息。因此,我需要一个解决方案来创建一个只有列名的空 DataFrame。
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.
PS:列名仍会出现在 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.
“空数据帧”部分很好!但是我仍然需要显示列而不是索引。
Edit:
编辑:
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:
我发现了一件重要的事情:我正在使用 Jinja2 将此 DataFrame 转换为 PDF,因此我调用了一种方法来首先将其输出为 HTML,如下所示:
df.to_html()
This is where the columns get lost I think.
我认为这就是列丢失的地方。
Edit2: In general, I followed this example: http://pbpython.com/pdf-reports.html. The css is also from the link. That's what I do to send the dataframe to the PDF:
Edit2:一般来说,我遵循这个例子:http: //pbpython.com/pdf-reports.html。css 也来自链接。这就是我将数据框发送到 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"])
Edit3:
编辑3:
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:
这似乎很合理,但如果我打印出 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:
E4:如果我打印出以下内容:
print(df.to_html())
I get the following result already:
我已经得到以下结果:
<table border="1" class="dataframe">
<tbody>
<tr>
<td>Index([], dtype='object')</td>
<td>Empty DataFrame</td>
</tr>
</tbody>
</table>
回答by Marcus V.
You can create an empty DataFrame with either column names or an Index:
您可以使用列名或索引创建一个空的 DataFrame:
In [4]: import pandas as pd
In [5]: df = pd.DataFrame(columns=['A','B','C','D','E','F','G'])
In [6]: df
Out[6]:
Empty DataFrame
Columns: [A, B, C, D, E, F, G]
Index: []
Or
或者
In [7]: df = pd.DataFrame(index=range(1,10))
In [8]: df
Out[8]:
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:
编辑:即使在您修改 .to_html 之后,我也无法重现。这个:
df = pd.DataFrame(columns=['A','B','C','D','E','F','G'])
df.to_html('test.html')
Produces:
产生:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
回答by Linda
Are you looking for something like this?
你在寻找这样的东西吗?
COLUMN_NAMES=['A','B','C','D','E','F','G']
df = pd.DataFrame(columns=COLUMN_NAMES)
df.columns
Index(['A', 'B', 'C', 'D', 'E', 'F', 'G'], dtype='object')
回答by Eric
df.to_html()
has a columns parameter.
df.to_html()
有一个列参数。
Just pass the columns into the to_html()
method.
只需将列传递到to_html()
方法中即可。
df.to_html(columns=['A','B','C','D','E','F','G'])