首页 > 其他分享 >Pandas教程之三十五:XlsxWriter使用详解(2)

Pandas教程之三十五:XlsxWriter使用详解(2)

时间:2024-12-21 19:00:08浏览次数:9  
标签:writer format XlsxWriter object datetime date 三十五 Pandas

Python | 使用 Pandas 和 XlsxWriter | 集合 – 2

Python Pandas 是一个数据分析库。它可以读取、过滤和重新排列小型和大型数据集,并以包括 Excel 在内的多种格式输出它们。

Pandas使用 XlsxWriter 模块写入 Excel 文件。

XlsxWriter是一个用于以 XLSX 文件格式写入文件的 Python 模块。它可用于将文本、数字和公式写入多个工作表。此外,它还支持格式化、图像、图表、页面设置、自动筛选、条件格式等功能。

代码#1: 使用 Pandas 和 XlsxWriter 将具有日期时间的 Pandas 数据框转换为具有默认日期时间和日期格式的 Excel 文件。

# import pandas library as pd

import pandas as pd

   

# from datetime module import

# datetime and date method

from datetime import datetime, date

   

# Create a Pandas dataframe from some datetime data.

# datetime(year,month,date,hour,minute,second)

# date(year,month,date)

dataframe = pd.DataFrame({

    'Date and time': [ datetime(2018, 1, 11, 11, 30, 55),

                      datetime(2018, 2, 12, 1,  20, 33),

                      datetime(2018, 3, 13, 11, 10    ),

                      datetime(2018, 4, 14, 16, 45, 35),

                      datetime(2018, 5, 15, 12, 10, 15)],

                      

   'Dates only':    [ date(2018, 6, 21),

                      date(2018, 7, 22),

                      date(2018, 8, 23),

                      date(2018, 9, 24),

                      date(2018, 10, 25) ], })

   

# Create a Pandas Excel writer 

# object using XlsxWriter as the engine.

# Also set the default datetime and date formats.

   

# mmmm dd yyyy => month date year

# month - full name, date - 2 digit, year - 4 digit

   

# mmm d yyyy hh:mm:ss => month date year hour: minute: second

# month - first 3 letters , date - 1 or 2 digit , year - 4 digit.

writer_object = pd.ExcelWriter("Example_datetime.xlsx",

                        engine ='xlsxwriter',

                        datetime_format ='mmm d yyyy hh:mm:ss',

                        date_format ='mmmm dd yyyy')

   

# Write a dataframe to the worksheet. 

dataframe.to_excel(writer_object, sheet_name ='Sheet1')

   

# Create xlsxwriter worksheet object

worksheet_object  = writer_object.sheets['Sheet1']

   

# set width of the B and C column

worksheet_object.set_column('B:C', 20)

   

# Close the Pandas Excel writer 

# object and output the Excel file. 

writer_object.save()

输出:


 
代码#2:使用 Pandas 和 XlsxWriter 将 Pandas 数据框转换为具有列格式的 Excel 文件。

# import pandas lib as pd

import pandas as pd

   

# Create a Pandas dataframe from some data.

dataframe = pd.DataFrame(

    {'Marks (Out of 50)': [30, 40, 45, 15, 8, 5, 35],

     'Percentage': [.6,   .8,   .9,  .3,  .16,   .1,  .7 ], })

   

# Create a Pandas Excel writer 

# object using XlsxWriter as the engine. 

writer_object = pd.ExcelWriter("Example_column.xlsx",

                                engine ='xlsxwriter')

   

# Write a dataframe to the worksheet. 

dataframe.to_excel(writer_object, sheet_name ='Sheet1')

   

# Create xlsxwriter workbook object .

workbook_object = writer_object.book

   

# Create xlsxwriter worksheet object

worksheet_object = writer_object.sheets['Sheet1']

   

# Create a new Format object to formats cells 

# in worksheets using add_format() method .

   

# number taken upto 2 decimal places

# format object is create.

format_object1 = workbook_object.add_format({'num_format': '# 0.00'})

   

# Integral percentage format object is create.

format_object2 = workbook_object.add_format({'num_format': '0 %'})

   

# Note: It isn't possible to format

# any cells that already have a format

# such as the index or headers or any

# cells that contain dates or datetimes.

   

# Set the column width and format.

worksheet_object.set_column('B:B', 20, format_object1)

   

# Set the column width and format.

worksheet_object.set_column('C:C', 15, format_object2)

   

# Close the Pandas Excel writer 

# object and output the Excel file. 

writer_object.save()

输出:


 
代码#3:使用 Pandas 和 XlsxWriter 将 Pandas 数据框转换为具有用户定义标题格式的 Excel 文件。

# import pandas lib as pd

import pandas as pd

   

   

data1 = ["Math", "Physics", "Computer",

         "Hindi", "English", "chemistry"]

data2 = [95, 78, 80, 80, 60, 95]

data3 = [90, 67, 78, 70, 63, 90]

   

# Create a Pandas dataframe from some data.

dataframe = pd.DataFrame(

    {'Subject': data1,

     'Mid Term Exam Scores Out of 100' : data2,

     'End Term Exam Scores Out of 100' : data3})

   

# Create a Pandas Excel writer 

# object using XlsxWriter as the engine. 

writer_object = pd.ExcelWriter("Example_header.xlsx",

                                engine ='xlsxwriter')

   

# Write a dataframe to the worksheet. 

# we turn off the default header

# and skip one row because we want

# to insert a user defined header there.

dataframe.to_excel(writer_object, sheet_name ='Sheet1', 

                          startrow = 1, header = False)

   

# Create xlsxwriter workbook object .

workbook_object = writer_object.book

   

# Create xlsxwriter worksheet object

worksheet_object = writer_object.sheets['Sheet1']

   

# Create a new Format object to formats cells 

# in worksheets using add_format() method .

   

# here we create a format object for header.

header_format_object = workbook_object.add_format({

                                'bold': True,

                                'italic' : True,

                                'text_wrap': True,

                                'valign': 'top',

                                'font_color': 'red',

                                'border': 2})

   

# Write the column headers with the defined format.

for col_number, value in enumerate(dataframe.columns.values):

    worksheet_object.write(0, col_number + 1, value, 

                              header_format_object)

   

# Close the Pandas Excel writer 

# object and output the Excel file. 

writer_object.save()

输出 :

标签:writer,format,XlsxWriter,object,datetime,date,三十五,Pandas
From: https://blog.csdn.net/xt14327/article/details/144618878

相关文章

  • 用pandas读取MRPC数据库时报错:pandas.errors.ParserError: Error tokenizing data. C
    读取的代码很简单,如下:data_path='MRPC/msr_paraphrase_test.txt'df=pd.read_csv(data_path,sep='\t',encoding='utf-8')困扰了一下午,最后本来不打算解决了。想着直接跳过错误,即:df=pd.read_csv(data_path,sep='\t',encoding='utf-8',on_......
  • python数据分析:介绍pandas库的数据类型Series和DataFrame
    安装pandaspipinstallpandas-ihttps://mirrors.aliyun.com/pypi/simple/使用pandas直接导入即可importpandasaspdpandas的数据结构pandas提供了两种主要的数据结构:Series和DataFrame,类似于python提供list列表,dict字典,tuple元组等数据类型用于存储数据。1.Se......
  • 【Pandas】pandas Series nbytes
    Pandas2.2SeriesAttributes方法描述Series.index每个数据点的标签或索引Series.array对象底层的数据数组Series.values以NumPy数组的形式访问Series中的数据值Series.dtype用于获取PandasSeries中数据的类型(dtype)Series.shape用于获取PandasSeries的形状,即其维度信......
  • 【Pandas】pandas Series nbytes
    Pandas2.2SeriesAttributes方法描述Series.index每个数据点的标签或索引Series.array对象底层的数据数组Series.values以NumPy数组的形式访问Series中的数据值Series.dtype用于获取PandasSeries中数据的类型(dtype)Series.shape用于获取PandasSeries的形状,即其维度信......
  • PandasAI --- Open Source AI Agents for Data Analysis
    PandasAIhttps://pandas-ai.com/  https://github.com/Sinaptik-AI/pandas-aiChatwithyourdatabase(SQL,CSV,pandas,polars,mongodb,noSQL,etc).PandasAImakesdataanalysisconversationalusingLLMs(GPT3.5/4,Anthropic,VertexAI)andRAG.Pa......
  • SQLAlchemy与Pandas版本差异中的URL字符问题:quote_plus来解决
    在数据科学和工程领域,SQLAlchemy和Pandas是两个极其常用的Python库。SQLAlchemy是一个强大的SQL工具包和对象关系映射(ORM)库,而Pandas则是数据处理和分析的必备工具。然而,在使用这两个库进行数据库操作时,特别是当它们的版本之间存在较大差异时,你可能会遇到一些字符处理的问题,尤其是......
  • 【机器学习】数据分析之Pandas(一)
    pandas数据分析库第一部分介绍Python在数据处理和准备方面一直做得很好,但在数据分析和建模方面就差一些。pandas帮助填补了这一空白,使您能够在Python中执行整个数据分析工作流程,而不必切换到更特定于领域的语言,如R。与出色的jupyter工具包和其他库相结合,Python中用于进......
  • 【数据分析:超实用的pandas语法技巧(一)】
    前言:......
  • 【Pandas】pandas Series values
    Pandas2.2SeriesAttributes方法描述Series.index每个数据点的标签或索引Series.array对象底层的数据数组Series.values以NumPy数组的形式访问Series中的数据值pandas.Series.valuespandas.Series.values属性是Pandas库中Series对象的一个重要属性,它允许你以NumPy......
  • 【Pandas】pandas Series array
    Pandas2.2SeriesAttributes方法描述Series.index每个数据点的标签或索引Series.array对象底层的数据数组Series.values以NumPy数组的形式访问Series中的数据值pandas.Series.arraypandas.Series.array属性是Pandas库中的一个功能,它允许你访问Series对象底层的数据......