将数据帧中的201729格式转换为新列中的月份,其中日期是第一个星期一(Convert a Week format like 201729 in a dataframe to a month in a

编程入门 行业动态 更新时间:2024-10-26 18:26:52
将数据帧中的201729格式转换为新列中的月份,其中日期是第一个星期一(Convert a Week format like 201729 in a dataframe to a month in a new column where the date is the first monday)

我找到了这个,但不能正确的语法。

time.asctime(time.strptime('2017 28 1', '%Y %W %w'))

我想设置一个新列,以7月的格式“201707”显示月份。 它可以是int64或字符串不必是列中的实际可读日期。

我的数据框列['Week']也采用201729格式,即YYYYWW

dfAttrition_Billings_KPIs['Day_1'] = \ time.asctime(time.strptime(dfAttrition_Billings_KPIs['Week'].str[:4] + dfAttrition_Billings_KPIs['Month'].str[:-2] - 1 + 1', '%Y %W %w'))

所以我希望201729周的行的输出显示在201707的新字段中。输出取决于'Week'中行值的含义。

我有一百万条记录,所以想尽可能避免迭代行,lambdas和慢函数:)

I found this but cant get the syntax correct.

time.asctime(time.strptime('2017 28 1', '%Y %W %w'))

I want to set a new column to show month in the format "201707" for July. It can be int64 or string doesnt have to be an actual readable date in the column.

My dataframe column ['Week'] is also in the format 201729 i.e. YYYYWW

dfAttrition_Billings_KPIs['Day_1'] = \ time.asctime(time.strptime(dfAttrition_Billings_KPIs['Week'].str[:4] + dfAttrition_Billings_KPIs['Month'].str[:-2] - 1 + 1', '%Y %W %w'))

So I want the output of the rows that have week 201729 to show in a new field month 201707. the output depends on what the row value is in 'Week'.

I have a million records so would like to avoid iterations of rows, lambdas and slow functions where possible :)

最满意答案

使用参数format to_datetime , Mondays加1 ,格式YYYYMM使用strftime

df = pd.DataFrame({'date':[201729,201730,201735]}) df['date1']=pd.to_datetime(df['date'].astype(str) + '1', format='%Y%W%w') df['date2']=pd.to_datetime(df['date'].astype(str) + '1', format='%Y%W%w').dt.strftime('%Y%m') print (df) date date1 date2 0 201729 2017-07-17 201707 1 201730 2017-07-24 201707 2 201735 2017-08-28 201708

如果需要从datetime转换为weeks自定义格式:

df = pd.DataFrame({'date':pd.date_range('2017-01-01', periods=10)}) df['date3'] = df['date'].dt.strftime('%Y %W %w') print (df) date date3 0 2017-01-01 2017 00 0 1 2017-01-02 2017 01 1 2 2017-01-03 2017 01 2 3 2017-01-04 2017 01 3 4 2017-01-05 2017 01 4 5 2017-01-06 2017 01 5 6 2017-01-07 2017 01 6 7 2017-01-08 2017 01 0 8 2017-01-09 2017 02 1 9 2017-01-10 2017 02 2

Use to_datetime with parameter format with add 1 for Mondays, last for format YYYYMM use strftime

df = pd.DataFrame({'date':[201729,201730,201735]}) df['date1']=pd.to_datetime(df['date'].astype(str) + '1', format='%Y%W%w') df['date2']=pd.to_datetime(df['date'].astype(str) + '1', format='%Y%W%w').dt.strftime('%Y%m') print (df) date date1 date2 0 201729 2017-07-17 201707 1 201730 2017-07-24 201707 2 201735 2017-08-28 201708

If need convert from datetime to weeks custom format:

df = pd.DataFrame({'date':pd.date_range('2017-01-01', periods=10)}) df['date3'] = df['date'].dt.strftime('%Y %W %w') print (df) date date3 0 2017-01-01 2017 00 0 1 2017-01-02 2017 01 1 2 2017-01-03 2017 01 2 3 2017-01-04 2017 01 3 4 2017-01-05 2017 01 4 5 2017-01-06 2017 01 5 6 2017-01-07 2017 01 6 7 2017-01-08 2017 01 0 8 2017-01-09 2017 02 1 9 2017-01-10 2017 02 2

更多推荐

本文发布于:2023-08-07 13:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   转换为   星期   日期   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!