将一个excel文件里面具有相同参数的行提取后存入新的excel

复制代码
功能描述:
一个excel里面有很多行数据,其中“交易时间”这一列有很多交易日期,有些行的交易日期是一样的,那么就把所有交易日期相同的行挑出来,形成一个新的以交易日期命名的文件。




import pandas as pd
import os

# 读取原始Excel文件
df = pd.read_excel('原始文件.xlsx')

# 提取“交易时间”列
transaction_times = df['交易时间']
# 创建一个空的DataFrame用于存储具有相同“交易时间”的行
same_time_df = pd.DataFrame()
#新文件也是excel
file_extension = ".xlsx"
#新文件的存储路径
des = 'D:\\tmp_financial\DailyK'
# 使用交易时间迭代所有行
for time in transaction_times.unique():
    # 提取具有相同“交易时间”的行
    same_time_rows = df[df['交易时间'] == time]
    # 将这些行添加到新的DataFrame中
    same_time_df = same_time_df.append(same_time_rows)
    des_file = os.path.join(des,time + file_extension)
    same_time_df.to_excel(des_file, index=False)
    #清空这个帧,准备保留下一个相同交易时间的数据
    same_time_df = pd.DataFrame()
相关推荐
花酒锄作田10 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪14 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽14 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战15 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋20 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者2 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者2 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh2 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅2 天前
Python函数入门详解(定义+调用+参数)
python
曲幽2 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama