在excel数据文件中编写格式良好的pandas数据框的包
nicexcel的Python项目详细描述
nicexcel是一个轻量级的python包 这为熊猫的出口提供了可能 将本机数据帧对象转换为格式良好的MS Excel文件。
该包利用了pandas和openpyxl开源 python库添加一些简单的美学细节以输出excel文件。 这提高了文件的整体方面和可用性,因此 有助于报告活动的自动化。
- 列宽自动调整以适合其中包含的字符
- 包含Excel中可筛选列的标题行(默认情况下)
- 标题行冻结(默认)
- 默认无索引(VS^ {STR 1 } $熊猫< /强>默认索引设置)
- 输出excel文件中的列的格式类似于 pandas默认数据类型。但是,仍然可以指定一些 通过特定参数的数字格式
开始使用to_excel()
nicexcel包的主要功能包括 pandas.dataframe对象。将to_excel()方法设想为 流行的pd.dataframe.to_excel()的包装,需要两个 关键输入:
- df:pd.dataframe实例
- filename:包含输出的文件路径的字符串 .xlsx文件将被保存
下面是一个简单的应用程序示例
importnicexcelasnlimportpandasaspd# instantiate mock datamock_df=pd.DataFrame(data={'Product Line':["T-Shirts","Jeans","Jackets"],'Units sold':[11213,9908,2345],'Yearly Revenues':[30000.0411,45513.1228,10220.103],'Gross Margin (%)':[0.223,0.1151,0.458]})# column formatcols_format={'Integer':['Yearly Income'],'%':['Gross Margin (%)']}# generate nicely formatted excelnl.to_excel(df=mock_df,filename='output.xlsx',cols_format=cols_format)
第二种方法:将转换为excel ms()
以类似的方式,nicexcel.to_excel_ms()允许 导出一组不同的pd.dataframe 对象到不同工作表上的相同输出.xlsx文件。
该方法有两个主要参数
- dfs:pd.dataframe实例的dict
- filename:包含输出的文件路径的字符串 .xlsx文件将被保存
请参阅下面的应用程序示例。
importnicexcelasnlimportpandasaspd# get sample datatx_df=pd.DataFrame(data={'date':['11/08/2018','09/04/2018','30/03/2018'],'customer':['XYZ Corporation','ABC Electric','XYZ Corporation'],'price':[113.202,220.99,90.1011]})cust_df=pd.DataFrame(data={'customer':['ABC Electric','XYZ Corporation'],'city':['London','Tokyo'],'revenue (M)':[100.2334,76.23216]})# initiate dict of dataframesdfs={'transactional_db':tx_df,'customer_db':cust_df}# generate nicely formatted excel filenl.to_excel_ms(dfs=dfs,filename='output_ms')
)
文件
文档在提供的docstrings中提供 代码。