Skip to content Skip to sidebar Skip to footer

How To Format Columns Dates In Python That They Are Weekly Based On Eachother?

I have a dataframe df that looks similar to this: identity Start End week E 6/18/2020 7/2/2020 1 E 6/18/2020 7/2/2020 2 2D 7/

Solution 1:

Similar to your other question:

First convert to datetimes:

df.loc[:, ["Start", "End"]]=(df.loc[:, ["Start", "End"]].transform(pd.to_datetime,format="%m/%d/%Y"))dfidentityStartEndweek0E2020-06-18  2020-07-02  11E2020-06-18  2020-07-02  222D2020-07-18  2020-08-01  132D2020-07-18  2020-08-01  24A12020-09-06  2020-09-20  15A12020-09-06  2020-09-20  2

Your identity is in groups of two, so I'll use that when selecting dates from the date_range:

from itertools import chain

result= df.drop_duplicates(subset="identity")

date_range = (
    pd.date_range(start, end, freq="7D")[:2]
    forstart, endin zip(result.Start, result.End)
)

date_range = chain.from_iterable(date_range)
End= lambda df: df.Start.add(pd.Timedelta("7 days"))

Create new dataframe:

df.assign(Start=list(date_range),End=End)identityStartEndweek0E2020-06-18  2020-06-25  11E2020-06-25  2020-07-02  222D2020-07-18  2020-07-25  132D2020-07-25  2020-08-01  24A12020-09-06  2020-09-13  15A12020-09-13  2020-09-20  2

Post a Comment for "How To Format Columns Dates In Python That They Are Weekly Based On Eachother?"