[英]How to consider previous month first day and previous month last day in timestamp (13 digits--milliseconds) format in python 2.7 and python 3x?
[英]Last Day previous Month
我有这个数据框
import pandas as pd
df = pd.DataFrame({'Found':['A','A','A','A','A','B','B','B'],
'Date':['14/10/2021','19/10/2021','29/10/2021','30/09/2021','20/09/2021','20/10/2021','29/10/2021','15/10/2021','10/09/2021'],
'LastDayMonth':['29/10/2021','29/10/2021','29/10/2021','30/09/2021','30/09/2021','29/10/2021','29/10/2021','29/10/2021','30/09/2021'],
'Mark':[1,2,3,4,3,1,2,3,2]
})
print(df)
Found Date LastDayMonth Mark
0 A 14/10/2021 29/10/2021 1
1 A 19/10/2021 29/10/2021 2
2 A 29/10/2021 29/10/2021 3
3 A 30/09/2021 30/09/2021 4
4 A 20/09/2021 30/09/2021 3
5 B 20/10/2021 29/10/2021 1
6 B 29/10/2021 29/10/2021 2
7 B 15/10/2021 29/10/2021 3
8 B 10/09/2021 30/09/2021 2
基于此数据框,我需要创建一个新列,该列是该月最后一天的“标记”以形成此新列。
也就是说,我需要每个 Found 月份最后一天的“Mark”列的值
我是怎么做的
mark_last_day = df.loc[df.apply(lambda x: x['Date']==x['LastDayMonth'], 1)]
df.merge(mark_last_day[['Found', 'LastDayMonth', 'Mark']],
how='left',
on=['Found', 'LastDayMonth'],
suffixes=('', '_LastDayMonth'))
# Output
Found Date LastDayMonth Mark Mark_LastDayMonth
0 A 14/10/2021 29/10/2021 1 3
1 A 19/10/2021 29/10/2021 2 3
2 A 29/10/2021 29/10/2021 3 3
3 A 30/09/2021 30/09/2021 4 4
4 A 20/09/2021 30/09/2021 3 4
5 B 20/10/2021 29/10/2021 1 2
6 B 29/10/2021 29/10/2021 2 2
7 B 15/10/2021 29/10/2021 3 2
到目前为止一切顺利,但我无法使用上个月的 Mark_LastDayMonth 创建一个新列,或者我需要当月和上个月的最后一天我该怎么做
前任。
Found Date LastDayMonth Mark Mark_LastDayMonth Mark_LastDayPrevious_Month
0 A 14/10/2021 29/10/2021 1 3 4
1 A 19/10/2021 29/10/2021 2 3 4
2 A 29/10/2021 29/10/2021 3 3 4
3 A 30/09/2021 30/09/2021 4 4 x
4 A 20/09/2021 30/09/2021 3 4 x
5 B 20/10/2021 29/10/2021 1 2 1
6 B 29/10/2021 29/10/2021 2 2 1
7 B 15/10/2021 29/10/2021 3 2 1
8 B 10/09/2021 30/09/2021 1 1 x
使用日期偏移量MonthEnd
from pandas.tseries.offsets import MonthEnd
df['LastDayPreviousMonth'] = df['Date'] - MonthEnd()
>>> df[['Date', 'LastDayPreviousMonth']]
Date LastDayPreviousMonth
0 2021-10-14 2021-09-30
1 2021-10-19 2021-09-30
2 2021-10-29 2021-09-30
3 2021-09-30 2021-08-31
4 2021-09-20 2021-08-31
5 2021-10-20 2021-09-30
6 2021-10-29 2021-09-30
7 2021-10-15 2021-09-30
然后像您为“LastDayMonth”所做的那样进行类似的合并。
这是否有助于您完成解决方案?
注意:我假设 'Date' 和 'LastDayPreviousMonth' 是类似日期时间的。 如果不是,您需要先使用
df[['Date', 'LastDayMonth']] = df[['Date', 'LastDayMonth']].apply(pd.to_datetime)
这是一个获取上个月最后一天的函数
import datetime
def get_prev_month(date_str):
format_str = '%d/%m/%Y'
datetime_obj = datetime.datetime.strptime(date_str, format_str)
first_day_of_this_month = datetime_obj.replace(day=1)
last_day_of_prev_month = first_day_of_this_month - datetime.timedelta(days=1)
return last_day_of_prev_month.strftime("%d/%m/%Y")
这是一个从 mark_last_day 变量中获取任何日期标记的函数
def get_mark_of(date_str):
return mark_last_day[last_day_mark.Date==date_str].Mark
如果您想添加 LastDayPrevMonth 列除非您需要,否则您不需要这样做
df["LastDayPrevMonth"] = df.LastDayMonth.apply(lambda x: get_prev_month(x))
最后创建列 Mark_LastDayPrevMonth,如果数据集中不存在上个月,则设置为 0。
df["Mark_LastDayPrevMonth"] = df.LastDayMonth.apply(lambda x: get_mark_of(get_prev_month(x))).fillna(0).astype(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.