Advertisement

在Excel表中包含内容的行插入4个空白行

阅读量:
复制代码
 import pandas as pd

    
  
    
 # 读取Excel文件
    
 file_path = r'D:\vmflex_2500\大于10mm降水\降水分区5.xlsx'
    
 df = pd.read_excel(file_path)
    
  
    
 # 获取第一列有内容的行的索引
    
 non_empty_rows = df[df.iloc[:, 0].notna()].index
    
  
    
 # 插入4行空白行到有内容的行下面
    
 for row in reversed(non_empty_rows):
    
     df = pd.concat([df.iloc[:row+1], pd.DataFrame(index=range(4)), df.iloc[row+1:]], axis=0, ignore_index=True)
    
  
    
 # 将处理后的数据保存回Excel文件
    
 df.to_excel(file_path, index=False)

Import the pandas library, and refer to it by its abbreviated name pd. This allows for easy use

全部评论 (0)

还没有任何评论哟~