对保存在E:\Test\Test\Test内的所有EXCEL表格进行处理,日期列中的数据存储格式如2012-12-24,2013-12-24,2014-12-24,如何将开头为2012-XX-XX的数据全部删除并保存到新的excel表中
时间: 2024-02-18 19:59:56 浏览: 20
可以使用Python中的pandas库来处理这个问题,具体步骤如下:
1. 导入pandas库和需要的模块:
```python
import pandas as pd
import os
```
2. 设置文件路径和读取excel文件:
```python
file_path = r'E:\Test\Test\Test'
file_list = os.listdir(file_path) # 获取文件夹下的所有文件名
for file_name in file_list:
if file_name.endswith('.xlsx'):
file = os.path.join(file_path, file_name)
df = pd.read_excel(file)
```
3. 删除日期列中开头为2012-的行:
```python
df = df[~df['日期列'].str.startswith('2012-')]
```
4. 将处理后的数据保存到新的excel表中:
```python
new_file = os.path.join(file_path, 'new_' + file_name)
df.to_excel(new_file, index=False)
```
完整代码如下:
```python
import pandas as pd
import os
file_path = r'E:\Test\Test\Test'
file_list = os.listdir(file_path)
for file_name in file_list:
if file_name.endswith('.xlsx'):
file = os.path.join(file_path, file_name)
df = pd.read_excel(file)
df = df[~df['日期列'].str.startswith('2012-')]
new_file = os.path.join(file_path, 'new_' + file_name)
df.to_excel(new_file, index=False)
```
注意:需要根据实际情况修改日期列的列名。