import pandas as pd import datetime now = datetime.datetime.now().strftime('%Y%m%d') # 获取当前日期,格式为年月日 data = pd.read_excel(r'E:\每日数据\MISPOS明细.xlsx',dtype={'商户编号':str,'终端编号':str, '处理情况': str}) rows = data.shape[0] # 获取行数 shape[1]获取列数 department_list = [] for i in range(rows): temp = data["省份"][i] if temp not in department_list: department_list.append(temp) # 将省份的分类存在一个列表中 for department in department_list: new_df = pd.DataFrame() for i in range(0, rows): if data["省份"][i] == department: new_df = pd.concat([new_df, data.iloc[[i], :]], axis=0, ignore_index=True) # 上面的循环效率太低了,这么做太蠢了,其实可以用pandas的条件筛选 new_df = data[(data["省份"] == department) & (data["处理情况"] == "未处理")] # 如果多条件筛选,new_df = data[(data["省份"] == department)&()&()] 括号里写条件用 & 连起来 new_df.to_excel(str(department) +"旧机具未处理明细" + ".xlsx", sheet_name=department, index=False)
时间: 2024-04-27 18:19:18 浏览: 17
这段代码主要是读取一个Excel文件的数据,然后按照省份分类,将未处理的数据保存到不同的Excel文件中。具体的步骤如下:
1. 导入pandas和datetime模块
2. 获取当前日期并格式化为年月日的形式
3. 使用pandas读取Excel文件,其中dtype指定了一些列的数据类型,包括商户编号、终端编号和处理情况,这些列会被转换为字符串类型
4. 获取数据的行数和列数
5. 创建一个空列表department_list,用于存储不同的省份
6. 遍历数据,将省份分类存储到department_list中
7. 遍历department_list,按照省份条件筛选数据,并将未处理的数据保存到不同的Excel文件中。具体的保存方式是使用pandas的to_excel方法,将数据保存到Excel文件中,文件名为"省份名称+旧机具未处理明细.xlsx",sheet名称也为省份名称。同时,参数index=False表示不保存行索引。
需要注意的是,代码中的第11-15行使用了循环方式来筛选数据,效率比较低,可以使用pandas的条件筛选方式来优化。
相关问题
pd.to_datetime(date).strftime("%Y%m%d")
这是一个Python pandas库中的函数,它将日期或日期字符串转换为指定格式的字符串。具体地,它将一个日期对象或日期字符串转换为年月日(%Y%m%d)的字符串格式。其中,%Y代表四位数的年份,%m代表两位数的月份,%d代表两位数的日期。例如,如果date为"2022-01-01",则该函数将返回"20220101"。
帮我重写如下python代码,#coding=utf-8-sig import re import os import xlrd import xlwt import time import datetime import math #import pandas import sys import shutil #通用部分 def time_mc(number_t): localTime = time.strftime("%Y%m%d%H%M%S", time.localtime()) time_mc_n
当然可以,以下是重写后的代码:
```python
#coding=utf-8-sig
import re
import os
import xlrd
import xlwt
import time
import datetime
import math
import sys
import shutil
# 通用部分
def time_mc(number_t):
localTime = time.strftime("%Y%m%d%H%M%S", time.localtime())
time_mc_n = localTime + str(number_t)
return time_mc_n
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def del_file(path):
if os.path.exists(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def get_file_path(root_path, file_type):
file_list = []
for dirpath, dirnames, filenames in os.walk(root_path):
for filename in filenames:
if filename.endswith(file_type):
file_list.append(os.path.join(dirpath, filename))
return file_list
# 主程序
if __name__ == '__main__':
pass
```
我将原来的代码中的注释进行了调整,并且将一些通用函数进行了函数封装,以方便代码的复用性和可读性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)