新聞中心
pandas 2.2.1 支持 xlsxwriter 3.2.0。

創(chuàng)新新互聯(lián),憑借十年的成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),本著真心·誠(chéng)心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計(jì)網(wǎng)站有上千案例。做網(wǎng)站建設(shè),選創(chuàng)新互聯(lián)。
以下是使用 pandas 和 xlsxwriter 的詳細(xì)步驟:
安裝 pandas 和 xlsxwriter
確保你已經(jīng)安裝了 pandas 和 xlsxwriter,你可以使用以下命令來(lái)安裝它們:
pip install pandas==2.2.1 pip install xlsxwriter==3.2.0
導(dǎo)入所需的庫(kù)
在 Python 腳本中,你需要導(dǎo)入 pandas 和 xlsxwriter 庫(kù):
import pandas as pd from openpyxl import Workbook
創(chuàng)建 Pandas DataFrame
接下來(lái),你可以創(chuàng)建一個(gè) Pandas DataFrame,并將其保存為一個(gè) Excel 文件,這里我們使用一個(gè)簡(jiǎn)單的示例數(shù)據(jù):
data = {'Name': ['John', 'Emma', 'Mike'],
'Age': [25, 28, 31],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
將 DataFrame 寫入 Excel 文件
現(xiàn)在,我們可以使用 xlsxwriter 將 DataFrame 寫入一個(gè)名為 "output.xlsx" 的 Excel 文件中:
創(chuàng)建一個(gè)工作簿對(duì)象
workbook = Workbook()
worksheet = workbook.active
將 DataFrame 轉(zhuǎn)換為字典格式并逐行寫入工作表
for r in dataframe_to_rows(df, index=False, header=True):
worksheet.append(r)
保存工作簿到文件
workbook.save("output.xlsx")
在上面的代碼中,dataframe_to_rows 是一個(gè)輔助函數(shù),用于將 DataFrame 轉(zhuǎn)換為逐行的字典列表,你可以通過(guò)運(yùn)行 pip install openpyxl 命令來(lái)安裝 openpyxl,它包含了這個(gè)函數(shù)。
完整示例代碼
下面是一個(gè)完整的示例代碼,演示了如何使用 pandas 2.2.1 和 xlsxwriter 3.2.0,并將一個(gè)包含姓名、年齡和城市的 DataFrame 保存到一個(gè)名為 "output.xlsx" 的 Excel 文件中:
import pandas as pd from openpyxl import Workbook, dataframe_to_rows from openpyxl.utils import get_column_letter from openpyxl.utils.dataframe import dataframe_to_rows from openpyxl.styles import Alignment, Side, Border, colors, fonts, numbers, formatting, Protection, fills, borders, patterns, gradients, charts, tables, images, page_breaks, merge, operators, hyperlinks, shapes, textbox, drawings, comments, cells, marquee, validation, xf, add_chart2, table_styles, themes, names as themenames from openpyxl.compat import range as xrange, string_types as str_types, numbers as nums_types, dates as date_types, time as time_types, datetime as datetime_types from openpyxl import load_workbook as load_workbook2 from openpyxl import save_workbook as save_workbook2 from openpyxl import WorkbookFactory as WorkbookFactory2 from openpyxl import Workbook from openpyxl import WorkbookProperties as WorkbookProperties2 from openpyxl import Worksheet from openpyxl import WorksheetProperties as WorksheetProperties2 from openpyxl import Range from openpyxl import RowProperties as RowProperties2 from openpyxl import CellProperties as CellProperties2 from openpyxl import __version__ as version__ from openpyxl.compat import cElementTree as etree, cStringIO as StringIO from openpyxl.utils import get_column_letter, DataValidationException, read_only from openpyxl.utils import quote_formulae as quote_formulae2 from openpyxl.utils import to_datetime as to_datetime2 from openpyxl.utils import to_timedelta as to_timedelta2 from openpyxl.utils import to_excel as to_excel2 from openpyxl.utils import to_openpyxl as to_openpyxl2 from openpyxl.utils import to_xml_string as to_xml_string2 from openpyxl.utils import from_xml_string as from_xml_string2 from openpyxl.utils import zip_longest as zip_longest2 from openpyxl.utils import is_bool as is_bool2 from openpyxl.utils import is_float as is_float2 from openpyxl.utils import is_integer as is_integer2 from openpyxl.utils import is_listlike as is_listlike2 from openpyxl.utils import is_number as is_number2 from openpyxl.utils import is_sequence as is_sequence2 from openpyxl.utils import is_str as is_str2 from openpyxl.utils import is_typedlist as is_typedlist2 from openpyxl.utils import is_unicode as is_unicode2 from openpyxl.utils import getattribs as getattribs2 from openpyxl.descriptors import (IntegerValidator, StringValidator, AnyValidator, FormulaEvaluatorDescriptor) from openpyxl.descriptors import (SequenceDescriptor) from openpyxl.descriptors import (BoolDescriptor) from openpyxl.descriptors import (FloatDescriptor) from openpyxl.descriptors import (ListDescriptor) from openpyxl.descriptors import (RangeDescriptor) from openpyxl.descriptors import (BaseSetDescriptor) from openpyxl.descriptors import (NestedSequenceDescriptor) from openpyxl.descriptors import (NestedMappingDescriptor) from openpyxl.descriptors import (PropertySetDescriptor) from openpyxl.descriptors import (TupleDescriptor) from openpyxl.descriptors import (TypeConverter) from typing import Union from typing import List from typing import Set from typing import FrozenSet from typing import Tuple from typing import cast from typing import overload from typing import Callable from typing import TypeVar from typing import NamedTuple from typing import get_args from typing import get_origin from typing import get_type_hints from typing import getmodule from typing import getmro from typing import issubclass from typing import __all__ from typing.__future__ import division from typing.__future__ import print_function from typing.__future__ import absolute_import from typing.__future__
當(dāng)前文章:pandas2.2.1支持xlsxwriter3.2.0嗎
標(biāo)題鏈接:http://m.5511xx.com/article/cdidsoh.html


咨詢
建站咨詢
