我想寫一個pandas資料框來合并某些列。我知道可以像這樣合并行:
df = pd.DataFrame({"animal": ("horse", "horse", "dog", "dog"), "color": ("black", "white", "grey", "black")})
df.to_excel("some_path_here")

但我不確定如何創建如下所示的輸出,其中列合并:

我試過這個:
df = pd.DataFrame({"animal": "color of fur", "horse": "black", "horse": "white", "dog": "grey", "dog": "black"})
df.to_excel("some_path_here")
但這不起作用,因為密鑰會相互覆寫。有小費嗎?
(這個例子不是最合乎邏輯的,但我會在我的專案中以不同的方式使用它)
uj5u.com熱心網友回復:
要創建如上所述的轉置輸出,請嘗試一個 2 階段程序,首先在 Pandas 中轉置資料,然后在動物相同時使用
您可以使用 openpyxl API 進一步調整和自定義 Excel 輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349864.html
