简体   繁体   中英

Using Python's pandas, split the date and select the most recent date

When I attempt to split the date(Ex: Date format: 10/13/2017-10/16/17), take the date after the hyphen, then modify the revised date into standard date format, I receive an error(KeyError: 'Date'). Below is the code:

import pandas as pd
import datetime as dt
File = pd.read_excel("Hypendata.xlsx")
before_symbol = File["Date"].str.split("-").str[1]
File["Modified data"] = pd.to_datetime(before_symbol["Date"]).dt.strftime("%m/%d/%Y")
File.to_excel("Hypendata.xlsx")

The problem I see is KeyError: "Date." "Date" is the header in my Excel document. I'm not sure why I keep getting this problem.

Could you help me with the code which helps to split the most recent date either it can be before Hypen or after hyphen. Example: 10/13/2017-10/16/17 In this case most recent date is after hyphen some dataset may have most recent date before hyphen as well.

Thank you.

before_symbol is a Series, you don't need access Date column

pd.to_datetime(before_symbol).dt.strftime("%m/%d/%Y")

To get the most recent date, you can try

File["Modified data"] = (File["Date"].str.split("-", expand=True)
                         .apply(pd.to_datetime)
                         .apply(lambda row: sorted(row)[-1], axis=1)
                         .dt.strftime("%m/%d/%Y"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM