![](/img/trans.png)
[英]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.