Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

Use the date offset 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’.

Does this help you complete the solution?

Note: I’m assuming ‘Date’ and ‘LastDayPreviousMonth’ are datetime-like. If they aren’t you need to convert them first using

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading