Python 熊猫df.to_带有CSS样式的html
有人能解释一下为什么下面没有输出数据吗?未生成Python 熊猫df.to_带有CSS样式的html,python,pandas,Python,Pandas,有人能解释一下为什么下面没有输出数据吗?未生成Results.html df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True) df.dropna(how="all", inplace=True) data = df.sort(ascending=True) HTML('''<style>.white_space_df td { white-space: normal; }</style>'
Results.html
df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True)
df.dropna(how="all", inplace=True)
data = df.sort(ascending=True)
HTML('''<style>.white_space_df td { white-space: normal; }</style>''')
HTML(data.to_html(os.path.join(root, "Results.html"), index=False, na_rep="NA", classes='white_space_df'))
df=pd.read\u csv(os.path.join(root,filename),skip\u blank\u lines=True)
df.dropna(how=“all”,inplace=True)
数据=df.sort(升序=真)
HTML(''.white_space_df td{white space:normal;}'')
HTML(data.to_HTML(os.path.join(root,“Results.HTML”),index=False,na_rep=“na”,class='white\u space\u df'))
更新
我自己也能解决这个问题,只要把其他人遇到的问题包起来,下面就是一个有效的例子
df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True)
df.dropna(how="all", inplace=True)
data = df.sort(ascending=True)
style = '''<style>.white_space_df td { word-wrap: break-word; }</style>'''
style + data.to_html(os.path.join(root, "Results.html"), index=False, na_rep="NA", classes='white_space_df')
df=pd.read\u csv(os.path.join(root,filename),skip\u blank\u lines=True)
df.dropna(how=“all”,inplace=True)
数据=df.sort(升序=真)
style='''.white_space_df td{word wrap:break word;}''
style+data.to_html(os.path.join(root,“Results.html”),index=False,na_rep=“na”,class='white\u space\u df')
to_html方法保存html文件(当传递buf
参数时),也就是说,它将xml保存到Results.html
。。。并且该方法返回None
data.to_html(os.path.join(root, "Results.html"), index=False, na_rep="NA", classes='white_space_df')
如果未传递buf
参数(第一个),它将返回一个字符串:
In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
In [12]: df.to_html()
Out[12]: '<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>A</th>\n <th>B</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1</td>\n <td>2</td>\n </tr>\n <tr>\n <th>1</th>\n <td>3</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>'
你确定你有df中的数据吗?如果我删除
HTML
只需要data.to_HTML(os.path.join(root,“Results.HTML”)、index=False、na_rep=“na”)就可以了
但我当然在尝试添加样式您正在使用的HTML
功能到底是什么?我在这里找到了这个想法,不管怎样,谢谢。我已经整理好了,我在我的帖子中发布了一个更新,不知道它去了哪里。但是我正要上床睡觉,等我明天不是僵尸了,我会仔细看看的。今天,以后。谢谢安迪
HTML(data.to_html(index=False, na_rep="NA", classes='white_space_df'))