简体   繁体   English

上个月的最后一天

[英]Last Day previous Month

I have this dataframe我有这个数据框

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

based on this dataframe I need to create a new column that is the "Mark" of the last day of the month to form this new column.基于此数据框,我需要创建一个新列,该列是该月最后一天的“标记”以形成此新列。

that is, I need the value of the 'Mark' column of the last day of the month of each Found也就是说,我需要每个 Found 月份最后一天的“Mark”列的值

how i did我是怎么做的

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

So far so good but I'm having trouble creating a new column with the Mark_LastDayMonth of the previous month or I need the last day of the current month and the previous month how do i do it到目前为止一切顺利,但我无法使用上个月的 Mark_LastDayMonth 创建一个新列,或者我需要当月和上个月的最后一天我该怎么做

Ex.前任。

    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

Use the date offset MonthEnd使用日期偏移量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

Then do a similarly merge as you did for 'LastDayMonth'.然后像您为“LastDayMonth”所做的那样进行类似的合并。

Does this help you complete the solution?这是否有助于您完成解决方案?

Note: I'm assuming 'Date' and 'LastDayPreviousMonth' are datetime-like.注意:我假设 'Date' 和 'LastDayPreviousMonth' 是类似日期时间的。 If they aren't you need to convert them first using如果不是,您需要先使用

df[['Date', 'LastDayMonth']] = df[['Date', 'LastDayMonth']].apply(pd.to_datetime)

Here is a function to get the last day of the previous month这是一个获取上个月最后一天的函数

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")

Here is a function to get the mark of any date from your mark_last_day variable这是一个从 mark_last_day 变量中获取任何日期标记的函数

def get_mark_of(date_str):
    return mark_last_day[last_day_mark.Date==date_str].Mark

If you want to add the LastDayPrevMonth column You don't need to do so unless you want it如果您想添加 LastDayPrevMonth 列除非您需要,否则您不需要这样做

df["LastDayPrevMonth"] = df.LastDayMonth.apply(lambda x: get_prev_month(x))

And at last the creating the column Mark_LastDayPrevMonth, and setting 0 if there exist no that previous month in the dataset.最后创建列 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM