如何将一些Excel文件作为数据帧导入,将日期更改为正确的格式,然后撤回每个文件中的最新日期

发布于 2025-02-05 11:47:13 字数 993 浏览 0 评论 0原文

我已经开始尝试学习python,以使我的工作中的各种任务更容易。我甚至能够将Excel文件读取到Pandas中,但是我希望能够在大约6个左右的文件中读取我必须定期刷新的文件,请扫描日期列,然后很好地告诉您我是每个文件的最新日期。这是我到目前为止的代码,但我在日期左右会遇到各种错误。文件中我的日期格式是'17/07/2021 13:53',尽管我可以看到有一些类似于'01/06/2022 10:48:02'。我真的只想提取日期并找出这些文件中存在的最新日期,以便我知道它们的最新时间。

pip.main(["install", "openpyxl","Jinja2"])

import re


import pandas as pd

from datetime import datetime

def date_convert(date_to_convert):
    return datetime.strptime(date_to_convert,'%d/%m/%Y %H:%M')
    #return datetime.strptime(date_to_convert,'%Y/%m/%d %H:%M:%S')
    #neither one works????

  



df_File1 = pd.read_excel (r'File1Location.xlsx')


df_File2 = pd.read_excel (r'File2Location.xlsx')



df_File1['Date String'] = df_File1['Date Created'].astype("string")
df_File1['Date'] = df_File1d['Date String'].apply(date_convert)





print ("File 1","\n",df_File1['Date Created'].max())




我想阅读大约5个左右的这些文件,并具有一个不错的整理输出,就像

-file 1:最新日期:27/04/2022

-file 2:最新日期:24/04/2022

等等等。

I have started trying to learn python to make various tasks within my job easier. I got as far as being able to read an excel file into pandas, but I want to be able to read in about 6 or so files that I have to refresh at regular intervals, have it scan the date column and then nicely and tidily tell me the latest date from each file. This is the code I have so far but I keep getting various errors around date. My date format in the file is '17/07/2021 13:53', although I can see that there are a few that are like '01/06/2022 10:48:02'. I really only want to extract the date and find out the latest date present in these files so I can know how up to date they are.

pip.main(["install", "openpyxl","Jinja2"])

import re


import pandas as pd

from datetime import datetime

def date_convert(date_to_convert):
    return datetime.strptime(date_to_convert,'%d/%m/%Y %H:%M')
    #return datetime.strptime(date_to_convert,'%Y/%m/%d %H:%M:%S')
    #neither one works????

  



df_File1 = pd.read_excel (r'File1Location.xlsx')


df_File2 = pd.read_excel (r'File2Location.xlsx')



df_File1['Date String'] = df_File1['Date Created'].astype("string")
df_File1['Date'] = df_File1d['Date String'].apply(date_convert)





print ("File 1","\n",df_File1['Date Created'].max())




I would like to read in about 5 or so of these files and have a nice tidy output that is like

-File 1 : Latest Date: 27/04/2022

-File 2 : Latest Date : 24/04/2022

etc and so on and so forth.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2025-02-12 11:47:13
import os
import pandas as pd

folder_path = 'the path of your folder goes here'
filenames = os.listdir(folder_path)

# Or, if you want only to open some files, uncomment next line:
# filenames = [r'File1Location.xlsx', r'File2Location.xlsx', r'File3Location.xlsx', r'File4Location.xlsx']

def print_date(filename):

    df_File = pd.read_excel(filename)

    df_File['Date'] = df_File['Date Created'].apply(pd.to_datetime, dayfirst=True)


    print(filename , ": ", df_File['Date Created'].max().strftime('%d/%m/%Y %H:%M'), "\n")

for filename in filenames:
    print_date(filename)
import os
import pandas as pd

folder_path = 'the path of your folder goes here'
filenames = os.listdir(folder_path)

# Or, if you want only to open some files, uncomment next line:
# filenames = [r'File1Location.xlsx', r'File2Location.xlsx', r'File3Location.xlsx', r'File4Location.xlsx']

def print_date(filename):

    df_File = pd.read_excel(filename)

    df_File['Date'] = df_File['Date Created'].apply(pd.to_datetime, dayfirst=True)


    print(filename , ": ", df_File['Date Created'].max().strftime('%d/%m/%Y %H:%M'), "\n")

for filename in filenames:
    print_date(filename)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文