| 
 | 
 
 
发表于 2024-6-28 22:14:34
|
显示全部楼层
 
 
 
 
 
好的,使用 `pandas` 库可以轻松完成这个任务。以下是一个示例代码,展示如何删除Excel中B列包含特定内容的行,并保存修改后的Excel文件: 
 
- import pandas as pd
 
  
- def delete_rows_with_specific_content(input_file, output_file, column_name, content):
 
 -     # 读取Excel文件
 
 -     df = pd.read_excel(input_file)
 
  
-     # 删除包含特定内容的行
 
 -     df = df[df[column_name] != content]
 
  
-     # 保存修改后的Excel文件
 
 -     df.to_excel(output_file, index=False)
 
  
- # 示例用法
 
 - input_file = 'path/to/your/input_file.xlsx'
 
 - output_file = 'path/to/your/output_file.xlsx'
 
 - column_name = 'B列名称'  # 这里应该是B列的实际列名
 
 - content = 'Unnamed: 1'
 
  
- delete_rows_with_specific_content(input_file, output_file, column_name, content)
 
  复制代码 
 
### 详细步骤: 
 
1. **导入`pandas`库**: 
   
 
2. **读取Excel文件**: 
  - df = pd.read_excel(input_file)
 
  复制代码 
   `input_file` 是输入Excel文件的路径。 
 
3. **删除包含特定内容的行**: 
  - df = df[df[column_name] != content]
 
  复制代码 
   这里 `column_name` 是B列的名称,`content` 是你要删除的内容。 
 
4. **保存修改后的Excel文件**: 
  - df.to_excel(output_file, index=False)
 
  复制代码 
   `output_file` 是输出Excel文件的路径。 
 
### 示例用法: 
 
假设你的输入文件路径是 `input_file.xlsx`,输出文件路径是 `output_file.xlsx`,B列的列名是 `Column B`,你要删除的内容是 `Unnamed: 1`: 
 
- input_file = 'input_file.xlsx'
 
 - output_file = 'output_file.xlsx'
 
 - column_name = 'Column B'  # 请根据实际情况替换为B列的列名
 
 - content = 'Unnamed: 1'
 
  
- delete_rows_with_specific_content(input_file, output_file, column_name, content)
 
  复制代码 
 
这样就可以删除B列中包含`Unnamed: 1`的所有行并将结果保存到新的Excel文件中。希望这能帮助到你! 
 
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |   
 
 
 
 |