Ada*_*ett 6 python dataframe pandas
我已经设计了数据帧输出的样式,并让它在 Jupyter Notebook 中显示我想要的方式,但我遇到问题,找不到一种将其保存为图像的好方法。我已经尝试过https://pypi.org/project/dataframe-image/但我的工作方式似乎是 NoneType 因为它是一个样式器对象,并且在尝试使用此库时出错。
这只是整个代码的一个片段,旨在循环几个“col_names”,我想将它们保存为图像(以解释一些编码)。
import pandas as pd
import numpy as np
col_name = 'TestColumn'
temp_df = pd.DataFrame({'TestColumn':['A','B','A',np.nan]})
t1 = (temp_df[col_name].fillna("Unknown").value_counts()/len(temp_df)*100).to_frame().reset_index()
t1.rename(columns={'index':' '}, inplace=True)
t1[' '] = t1[' '].astype(str)
display(t1.style.bar(subset=[col_name], color='#5e81f2', vmax=100, vmin=0).set_table_attributes('style="font-size: 17px"').set_properties(
**{'color': 'black !important',
'border': '1px black solid !important'}
).set_table_styles([{
'selector': 'th',
'props': [('border', '1px black solid !important')]
}]).set_properties( **{'width': '500px'}).hide_index().set_properties(subset=[" "], **{'text-align': 'left'}))
Run Code Online (Sandbox Code Playgroud)
能够更改我在样式器对象上使用数据帧图像的方式并使其正常工作。将其传递到 export() 函数而不是直接从对象中调用它似乎是执行此操作的正确方法。
.render() 确实获取了 HTML,但在将其转换为图像或不使用 Ipython HTML 显示查看时通常会丢失大部分样式。请参阅下面的比较。

工作代码:
import pandas as pd
import numpy as np
import dataframe_image as dfi
col_name = 'TestColumn'
temp_df = pd.DataFrame({'TestColumn':['A','B','A',np.nan]})
t1 = (temp_df[col_name].fillna("Unknown").value_counts()/len(temp_df)*100).to_frame().reset_index()
t1.rename(columns={'index':' '}, inplace=True)
t1[' '] = t1[' '].astype(str)
style_test = t1.style.bar(subset=[col_name], color='#5e81f2', vmax=100, vmin=0).set_table_attributes('style="font-size: 17px"').set_properties(
**{'color': 'black !important',
'border': '1px black solid !important'}
).set_table_styles([{
'selector': 'th',
'props': [('border', '1px black solid !important')]
}]).set_properties( **{'width': '500px'}).hide_index().set_properties(subset=[" "], **{'text-align': 'left'})
dfi.export(style_test, 'successful_test.png')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3960 次 |
| 最近记录: |