我有以下数据框:
year month week_num day
2019 8 31 Thurs
2019 8 31 Fri
2019 8 32 Tues
日期缩写为Mon
,Tues
,Weds
,Thurs
,Fri
,Sat
,Sun
。 / p>
我想生成另一列,以yyyy-mm-dd格式提供日期。我怎样才能做到这一点?预先感谢!
答案 0 :(得分:2)
datetime
模块为您提供了这个机会。 This discussion说明了如何从星期数中获取日期。
然后,您可以定义一个函数来获取日期并将其应用于您的数据框。
代码在这里:
# Import modules
import datetime
# Your data
df = pd.DataFrame([
[2019, 8, 29, "Fri"],
[2019, 8, 31, "Sun"],
[2019, 8, 29, "Tues"]],
columns=["year", "month", "week_num", "day"])
# A value per day
val_day = {"Mon": 0, "Tues": 1, "Weds": 2, "Thurs": 3,
"Fri": 4, "Sat": 5, "Sun": 6}
# Get the date from the year, number of week and the day
def getDate(row):
# Create string format
str_date = "{0}-W{1}-1".format(row.year,
row.week_num - 1)
print(str_date)
# Get the date
date = datetime.datetime.strptime(
str_date, "%Y-W%W-%w") + datetime.timedelta(days=val_day[row.day])
# Update date field
row["date"] = date.strftime("%Y-%m-%d")
return row
# apply the function to each row
df = df.apply(getDate, axis=1)
print(df)
# year month week_num day date
# 0 2019 8 1 Thurs 2019-01-03
# 1 2019 8 29 Fri 2019-07-19
# 2 2019 8 29 Tues 2019-07-16