英文:
GitHub doesn't reflect pandas styles
问题
GitHub无法反映我使用pandas方法style()应用于数据框的样式。
datapivothappiness_score[datapivothappiness_score.index.isin(short_list)].style\
.background_gradient(cmap='Greens')\
.format(precision=1, thousands=".", decimal=",")
在VSCode上运行正常。
但在GitHub上不起作用。
英文:
GitHub does not reflect the styles that I applied to the dataframe using the pandas method style().
datapivothappiness_score[datapivothappiness_score.index.isin (short_list)].style\
.background_gradient(cmap='Greens')\
.format(precision=1, thousands=".", decimal=",")
答案1
得分: 1
可能是因为GitHub以静态HTML形式呈现代码,而不是交互式笔记本,所以无法正确显示样式。如果您想查看带有应用样式的数据框,可以尝试将笔记本转换为其他格式,如HTML或PDF。
另外,您可以尝试使用to_html
方法将样式化的数据框转换为HTML,然后在GitHub上显示HTML。以下是如何执行此操作的示例:
styled_df = datapivothappiness_score[datapivothappiness_score.index.isin (short_list)].style\
.background_gradient(cmap='Greens')\
.format(precision=1, thousands=".", decimal=",")
html = styled_df.render()
# 将HTML保存到文件
with open('styled_dataframe.html', 'w') as f:
f.write(html)
# 在GitHub上显示HTML(必须放在反引号内)
print(f'`{html}`')
然后,将反引号内的HTML输出复制并粘贴到GitHub README文件中的Markdown单元格中,它应该能够正确显示。
请注意,此方法只会将样式化的数据框显示为静态HTML表格,不具有任何交互式功能。
英文:
It's possible that GitHub is not displaying the styles properly due to the fact that it's a static HTML rendering of your code and not an interactive notebook that can fully display the styles. You can try converting your notebook to another format such as HTML or PDF if you want to view your dataframe with the applied styles.
Alternatively, you can try using the to_html
method to convert your styled dataframe to HTML and then display the HTML on GitHub. Here is an example of how to do this:
styled_df = datapivothappiness_score[datapivothappiness_score.index.isin (short_list)].style\
.background_gradient(cmap='Greens')\
.format(precision=1, thousands=".", decimal=",")
html = styled_df.render()
# Save the HTML to a file
with open('styled_dataframe.html', 'w') as f:
f.write(html)
# Display the HTML on GitHub (must be placed within backticks)
print(f'`{html}`')
Then, copy and paste the HTML output within the backticks into a Markdown cell in your GitHub README file, and it should display properly.
Note that this method will only display the styled dataframe as a static HTML table and will not have any interactive features.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论