簡體   English   中英

如何在Excel中編寫Unicode對象

[英]How to write Unicode object in excel

我正在使用分組功能在excel中編寫數據框架。 我收到錯誤AttributeError: 'Unicode' object has no attribute 'to_excel'

數據框:

final_day_wise = daily_sales_data.loc [ : ,
                 ["Placement# Name", "Date", "Delivered Impressions", "Clicks", "CTR", "Conversion", "eCPA", "Spend"] ]

輸出:

startline = len(placement_sales_data) + len(final_adsize) + 18

for placement, placement_df in final_day_wise.groupby('Placement# Name'):
    writing_daily_data = placement.to_excel(self.config.writer, sheet_name="Standard banner({})".format(self.config.IO_ID), encoding='utf-8', startcol=1, startrow=startline, index=True, header=True, merge_cells=False)
    writing_daily_data_new = placement_df.to_excel(self.config.writer, sheet_name="Standard banner({})".format(self.config.IO_ID), startcol=1, startrow=startline + 1, index=False, header=True, merge_cells=False)

    startline += len(placement_df) + 4

寫入展示位置變量時出現錯誤,但是該錯誤不會出現在placement_df

我還可以根據索引對內容進行排序嗎?

當您在final_day_wise上進行groupby時,您將得到一個類似於object的字典,其中鍵是分組所在的列中的項(在本例中為“ Placement#name”),而值是groupby對象。 當您嘗試placement.to_excel()時,您會收到錯誤消息,因為放置是一個字符串,而不是數據框對象。

您期望placement是什么?

可以按索引排序。 例如,有一個df.sort_index 您想如何按索引排序?

編輯:

根據您的評論,這就是我認為您正在尋找的。

new_df = placement_df.copy()
new_df.columns = [
    [placement] * len(new_df.columns),
    [col for col in new_df.columns]
]

然后

new_df.to_excel (
    self.config.writer , 
    sheet_name="Standard banner({})".format(
        self.config.IO_ID
    ),
    startcol = 1,
    startrow = startline+1, 
    index = False,
    header = True,
    merge_cells = False)

startline += len(placement_df) + 4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM