[英]Combining year, month, week number and day to date
I have the below dataframe: 我有以下数据框:
year month week_num day
2019 8 31 Thurs
2019 8 31 Fri
2019 8 32 Tues
The day abbreviations are Mon
, Tues
, Weds
, Thurs
, Fri
, Sat
, Sun
. 当天缩写是Mon
, Tues
, Weds
, Thurs
, Fri
, Sat
, Sun
。
I want to generate another column which will provide me the date in yyyy-mm-dd format. 我想生成另一列,以yyyy-mm-dd格式提供日期。 How can I do that? 我怎样才能做到这一点? Thanks in advance! 提前致谢!
The module datetime
gives you this opportunity. 模块datetime
为您提供了这个机会。 This discussion explains how to get the date from the week number. 此讨论说明了如何从星期数获取日期。
Then, you can define a function to get the date and apply it to you dataframe. 然后,您可以定义一个函数以获取日期并将其应用于您的数据框。
Here the code: 这里的代码:
# 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.