在Excel中,在每一行数据下插入3个空行。
import pandas as pdimport os
cci_path = r'D:\daimaceshi\xinceshi20230501\CCI20230501.xlsx'
save_path = r'D:\daimaceshi\xinceshi20230601'
df = pd.read_excel(cci_path, sheet_name='分析表1')
writer = pd.ExcelWriter(os.path.join(save_path, 'CCI20230601.xlsx'))
df.to_excel(writer, sheet_name='分析1', index=False)
# 新建“date3”列,复制“date”列的数据到“date3”列
df['date3'] = df['date']
# 在每一个“date”数据行后插入3个空行
for i in range(len(df)):
for j in range(3):
df = pd.concat([df.iloc[:i+1], pd.Series(), df.iloc[i+1:]], ignore_index=True)
#df = pd.concat([df.iloc[:i+1], pd.Series(), df.iloc[i+1:]], ignore_index=True)
#df = pd.concat([df, df.loc[i]], ignore_index=True)
#inserted_rows = pd.DataFrame([{}], dtype='object')
#cci_data = pd.concat([cci_data, inserted_rows] * 3, ignore_index=True)
# cci_data.loc[cci_data.shape[0]] = pd.Series(dtype='object')
# cci_data.loc[cci_data.shape[0]] = pd.Series(dtype='object')
# cci_data.loc[cci_data.shape[0]] = pd.Series(dtype='object')
#for i in range(3):
#new_data.loc[new_data.shape[0]] =pd.serise(dtype='object')
#cci_data = pd.concat([cci_data, new_data], axis=0, ignore_index=True)
# 将结果保存到新Excel文件中
df.to_excel(writer, sheet_name='分析1', index=False)
writer._save()
试了好几种写法,都没有办法实现“在Excel的每一行下插入3个空行”,请高手赐教。