Cần hướng dẫn về cách tôi có thể định dạng một giá trị theo định dạng ngày trong Tôi chưa quen với Đầu ra của giá trị trông như thế này: Cần thiết để định dạng đầu ra theo định dạng ngày Tôi đã thử các tùy chọn của Cảm ơn bạn đã dành thời gian và sự giúp đỡ của bạn. Mã của tôi là một cái gì đó như thế này:
Công cụ mặc định để viết không thay đổi - đó vẫn là XLSXWriter, nhưng trong trường hợp này bạn không có XLSXWriter nên nó sử dụng OpenPyXL. Nếu bạn cài đặt xlsxwriter, bạn sẽ thấy hành vi trước đó. Đối với vấn đề - dường như là do những điều này ( ############################################################################## # # An example of converting a Pandas dataframe with datetimes to an xlsx file # with a default datetime and date format using Pandas and XlsxWriter. # # SPDX-License-Identifier: BSD-2-Clause # Copyright 2013-2022, John McNamara, # import pandas as pd from datetime import datetime, date # Create a Pandas dataframe from some datetime data. df = pd.DataFrame({'Date and time': [datetime(2015, 1, 1, 11, 30, 55), datetime(2015, 1, 2, 1, 20, 33), datetime(2015, 1, 3, 11, 10 ), datetime(2015, 1, 4, 16, 45, 35), datetime(2015, 1, 5, 12, 10, 15)], 'Dates only': [date(2015, 2, 1), date(2015, 2, 2), date(2015, 2, 3), date(2015, 2, 4), date(2015, 2, 5)], }) # Create a Pandas Excel writer using XlsxWriter as the engine. # Also set the default datetime and date formats. writer = pd.ExcelWriter("pandas_datetime.xlsx", engine='xlsxwriter', datetime_format='mmm d yyyy hh:mm:ss', date_format='mmmm dd yyyy') # Convert the dataframe to an XlsxWriter Excel object. df.to_excel(writer, sheet_name='Sheet1') # Get the xlsxwriter workbook and worksheet objects. in order to set the column # widths, to make the dates clearer. workbook = writer.book worksheet = writer.sheets['Sheet1'] # Get the dimensions of the dataframe. (max_row, max_col) = df.shape # Set the column widths, to make the dates clearer. worksheet.set_column(1, max_col, 20) # Close the Pandas Excel writer and output the Excel file. writer.save()2, ############################################################################## # # An example of converting a Pandas dataframe with datetimes to an xlsx file # with a default datetime and date format using Pandas and XlsxWriter. # # SPDX-License-Identifier: BSD-2-Clause # Copyright 2013-2022, John McNamara, # import pandas as pd from datetime import datetime, date # Create a Pandas dataframe from some datetime data. df = pd.DataFrame({'Date and time': [datetime(2015, 1, 1, 11, 30, 55), datetime(2015, 1, 2, 1, 20, 33), datetime(2015, 1, 3, 11, 10 ), datetime(2015, 1, 4, 16, 45, 35), datetime(2015, 1, 5, 12, 10, 15)], 'Dates only': [date(2015, 2, 1), date(2015, 2, 2), date(2015, 2, 3), date(2015, 2, 4), date(2015, 2, 5)], }) # Create a Pandas Excel writer using XlsxWriter as the engine. # Also set the default datetime and date formats. writer = pd.ExcelWriter("pandas_datetime.xlsx", engine='xlsxwriter', datetime_format='mmm d yyyy hh:mm:ss', date_format='mmmm dd yyyy') # Convert the dataframe to an XlsxWriter Excel object. df.to_excel(writer, sheet_name='Sheet1') # Get the xlsxwriter workbook and worksheet objects. in order to set the column # widths, to make the dates clearer. workbook = writer.book worksheet = writer.sheets['Sheet1'] # Get the dimensions of the dataframe. (max_row, max_col) = df.shape # Set the column widths, to make the dates clearer. worksheet.set_column(1, max_col, 20) # Close the Pandas Excel writer and output the Excel file. writer.save()3) arg
if_sheet_exists: str | none = none, ############################################################################## # # An example of converting a Pandas dataframe with datetimes to an xlsx file # with a default datetime and date format using Pandas and XlsxWriter. # # SPDX-License-Identifier: BSD-2-Clause # Copyright 2013-2022, John McNamara, # import pandas as pd from datetime import datetime, date # Create a Pandas dataframe from some datetime data. df = pd.DataFrame({'Date and time': [datetime(2015, 1, 1, 11, 30, 55), datetime(2015, 1, 2, 1, 20, 33), datetime(2015, 1, 3, 11, 10 ), datetime(2015, 1, 4, 16, 45, 35), datetime(2015, 1, 5, 12, 10, 15)], 'Dates only': [date(2015, 2, 1), date(2015, 2, 2), date(2015, 2, 3), date(2015, 2, 4), date(2015, 2, 5)], }) # Create a Pandas Excel writer using XlsxWriter as the engine. # Also set the default datetime and date formats. writer = pd.ExcelWriter("pandas_datetime.xlsx", engine='xlsxwriter', datetime_format='mmm d yyyy hh:mm:ss', date_format='mmmm dd yyyy') # Convert the dataframe to an XlsxWriter Excel object. df.to_excel(writer, sheet_name='Sheet1') # Get the xlsxwriter workbook and worksheet objects. in order to set the column # widths, to make the dates clearer. workbook = writer.book worksheet = writer.sheets['Sheet1'] # Get the dimensions of the dataframe. (max_row, max_col) = df.shape # Set the column widths, to make the dates clearer. worksheet.set_column(1, max_col, 20) # Close the Pandas Excel writer and output the Excel file. writer.save() |