1 回答
![?](http://img1.sycdn.imooc.com/54584f3100019e9702200220-100-100.jpg)
TA贡献1851条经验 获得超4个赞
要在同一个 Excel 中添加新闻表,您必须以追加模式打开文件。看看下面的代码:
def DuplicateEachRow():
import pandas as pd
import pathlib
full_path = str(pathlib.Path().absolute()) + '\\' + new_loc
df = pd.read_excel(full_path, header=None, sheet_name='GTL | GWL Disclosures')
print(df)
# duplicate the rows:
# keep the index, so you can sort the rows after
dup_df = pd.concat([df, df])
#sort the rows by the index so you have the duplicate one just after the initial one
dup_df.sort_index(inplace=True)
# using openpyxl
#open the file in append mode
with pd.ExcelWriter(new_loc, mode='a') as writer:
#use a new name for the new sheet
#don't save the header (dataframe columns names) and index (dataframe row names) in the new sheet
dup_df.to_excel(writer, sheet_name='Sheet3', header=None, index=None)
添加回答
举报