2 回答

TA貢獻(xiàn)1802條經(jīng)驗 獲得超4個贊
問題是您沒有在每個點都對數(shù)據(jù)框進(jìn)行切片。
利用 :
with pd.ExcelWriter('output.xlsx') as writer:
for number,each_col in enumerate(df.columns,1):
df[[each_col]].to_excel(writer,f'Sheet{number}',index=False)
或者如果您希望 shetname 成為列名。
with pd.ExcelWriter('output.xlsx') as writer:
for each_col in df.columns:
df[[each_col]].to_excel(writer,f'Sheet{each_col}',index=False)

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊
這應(yīng)該工作:
import pandas as pd
# created a dummy row in a new dataframe as per the column list you shared
df2 = pd.DataFrame(['Dummy_PassengerId1', 'Dummy_Survived1', 'Dummy_Pclass1', 'Dummy_Name1', 'Dummy_Sex1', 'Dummy_Age1', 'Dummy_SibSp1',
'Dummy_Parch1', 'Dummy_Ticket1', 'Dummy_Fare1', 'Dummy_Cabin1', 'Dummy_Embarked1']).T
# appended the appropriate headers to the dataframe
df2.columns = ['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']
以上兩個步驟只是創(chuàng)建一個包含一行和所需標(biāo)題的虛擬數(shù)據(jù)框。下面一行將滿足您的要求(據(jù)我所知):“將數(shù)據(jù)框的每一列寫入 excel 中的新工作表”。希望這能澄清。
with pd.ExcelWriter('output1.xlsx') as writer:
for col in df2.columns:
df2[col].to_excel(writer, sheet_name='Sheet_'+col)
添加回答
舉報