Create multiple columns based on date





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I started to work with pandas recently and during testing with 'date' I have found this challenge. Given this dataframe:



df = pd.DataFrame({'id': [123, 431, 652, 763, 234],
'time': ['8/1/2017', '6/1/2015', '7/1/2016', '9/1/2014', '12/1/2018']})



Create the new dataframe with backdate columns look like this:



    id        time       time1       time2       time3       time4      time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01


I tries with these codes:



df['time'] = pd.to_datetime(df['time'], errors='coerce') #Object to Date 
df['time1'] = df['time'] - pd.DateOffset(months=1)
df['time2'] = df['time'] - pd.DateOffset(months=2)
df['time3'] = df['time'] - pd.DateOffset(months=3)
df['time4'] = df['time'] - pd.DateOffset(months=4)
df['time5'] = df['time'] - pd.DateOffset(months=5)


Are there anyway to solve this problem faster and more efficient? I've already tested several methods to create the backdate. However I don't know how to do it with multiple columns. Because if the data requires to backdate 24 months, I have to copy and paste a lot (manually).










share|improve this question




















  • 1





    Possible duplicate of Offset date for a Pandas DataFrame date index

    – Stephen Rauch
    Jan 4 at 2:56


















2















I started to work with pandas recently and during testing with 'date' I have found this challenge. Given this dataframe:



df = pd.DataFrame({'id': [123, 431, 652, 763, 234],
'time': ['8/1/2017', '6/1/2015', '7/1/2016', '9/1/2014', '12/1/2018']})



Create the new dataframe with backdate columns look like this:



    id        time       time1       time2       time3       time4      time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01


I tries with these codes:



df['time'] = pd.to_datetime(df['time'], errors='coerce') #Object to Date 
df['time1'] = df['time'] - pd.DateOffset(months=1)
df['time2'] = df['time'] - pd.DateOffset(months=2)
df['time3'] = df['time'] - pd.DateOffset(months=3)
df['time4'] = df['time'] - pd.DateOffset(months=4)
df['time5'] = df['time'] - pd.DateOffset(months=5)


Are there anyway to solve this problem faster and more efficient? I've already tested several methods to create the backdate. However I don't know how to do it with multiple columns. Because if the data requires to backdate 24 months, I have to copy and paste a lot (manually).










share|improve this question




















  • 1





    Possible duplicate of Offset date for a Pandas DataFrame date index

    – Stephen Rauch
    Jan 4 at 2:56














2












2








2








I started to work with pandas recently and during testing with 'date' I have found this challenge. Given this dataframe:



df = pd.DataFrame({'id': [123, 431, 652, 763, 234],
'time': ['8/1/2017', '6/1/2015', '7/1/2016', '9/1/2014', '12/1/2018']})



Create the new dataframe with backdate columns look like this:



    id        time       time1       time2       time3       time4      time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01


I tries with these codes:



df['time'] = pd.to_datetime(df['time'], errors='coerce') #Object to Date 
df['time1'] = df['time'] - pd.DateOffset(months=1)
df['time2'] = df['time'] - pd.DateOffset(months=2)
df['time3'] = df['time'] - pd.DateOffset(months=3)
df['time4'] = df['time'] - pd.DateOffset(months=4)
df['time5'] = df['time'] - pd.DateOffset(months=5)


Are there anyway to solve this problem faster and more efficient? I've already tested several methods to create the backdate. However I don't know how to do it with multiple columns. Because if the data requires to backdate 24 months, I have to copy and paste a lot (manually).










share|improve this question
















I started to work with pandas recently and during testing with 'date' I have found this challenge. Given this dataframe:



df = pd.DataFrame({'id': [123, 431, 652, 763, 234],
'time': ['8/1/2017', '6/1/2015', '7/1/2016', '9/1/2014', '12/1/2018']})



Create the new dataframe with backdate columns look like this:



    id        time       time1       time2       time3       time4      time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01


I tries with these codes:



df['time'] = pd.to_datetime(df['time'], errors='coerce') #Object to Date 
df['time1'] = df['time'] - pd.DateOffset(months=1)
df['time2'] = df['time'] - pd.DateOffset(months=2)
df['time3'] = df['time'] - pd.DateOffset(months=3)
df['time4'] = df['time'] - pd.DateOffset(months=4)
df['time5'] = df['time'] - pd.DateOffset(months=5)


Are there anyway to solve this problem faster and more efficient? I've already tested several methods to create the backdate. However I don't know how to do it with multiple columns. Because if the data requires to backdate 24 months, I have to copy and paste a lot (manually).







python python-3.x pandas date dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 3:59







Long_NgV

















asked Jan 4 at 2:51









Long_NgVLong_NgV

426




426








  • 1





    Possible duplicate of Offset date for a Pandas DataFrame date index

    – Stephen Rauch
    Jan 4 at 2:56














  • 1





    Possible duplicate of Offset date for a Pandas DataFrame date index

    – Stephen Rauch
    Jan 4 at 2:56








1




1





Possible duplicate of Offset date for a Pandas DataFrame date index

– Stephen Rauch
Jan 4 at 2:56





Possible duplicate of Offset date for a Pandas DataFrame date index

– Stephen Rauch
Jan 4 at 2:56












1 Answer
1






active

oldest

votes


















2














Here is one way using date_range with concat



s=df.time.apply(lambda x : pd.date_range(end=x,periods =6,freq='MS')[::-1].tolist())
df=pd.concat([df,pd.DataFrame(s.tolist(),index=df.index).add_prefix('Time').iloc[:,1:]],axis=1)
df
id time Time1 Time2 Time3 Time4 Time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01





share|improve this answer
























  • Thank you sir, could you please explain a little bit about how to calculate 's'?

    – Long_NgV
    Jan 4 at 10:28












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032552%2fcreate-multiple-columns-based-on-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Here is one way using date_range with concat



s=df.time.apply(lambda x : pd.date_range(end=x,periods =6,freq='MS')[::-1].tolist())
df=pd.concat([df,pd.DataFrame(s.tolist(),index=df.index).add_prefix('Time').iloc[:,1:]],axis=1)
df
id time Time1 Time2 Time3 Time4 Time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01





share|improve this answer
























  • Thank you sir, could you please explain a little bit about how to calculate 's'?

    – Long_NgV
    Jan 4 at 10:28
















2














Here is one way using date_range with concat



s=df.time.apply(lambda x : pd.date_range(end=x,periods =6,freq='MS')[::-1].tolist())
df=pd.concat([df,pd.DataFrame(s.tolist(),index=df.index).add_prefix('Time').iloc[:,1:]],axis=1)
df
id time Time1 Time2 Time3 Time4 Time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01





share|improve this answer
























  • Thank you sir, could you please explain a little bit about how to calculate 's'?

    – Long_NgV
    Jan 4 at 10:28














2












2








2







Here is one way using date_range with concat



s=df.time.apply(lambda x : pd.date_range(end=x,periods =6,freq='MS')[::-1].tolist())
df=pd.concat([df,pd.DataFrame(s.tolist(),index=df.index).add_prefix('Time').iloc[:,1:]],axis=1)
df
id time Time1 Time2 Time3 Time4 Time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01





share|improve this answer













Here is one way using date_range with concat



s=df.time.apply(lambda x : pd.date_range(end=x,periods =6,freq='MS')[::-1].tolist())
df=pd.concat([df,pd.DataFrame(s.tolist(),index=df.index).add_prefix('Time').iloc[:,1:]],axis=1)
df
id time Time1 Time2 Time3 Time4 Time5
0 123 2017-08-01 2017-07-01 2017-06-01 2017-05-01 2017-04-01 2017-03-01
1 431 2015-06-01 2015-05-01 2015-04-01 2015-03-01 2015-02-01 2015-01-01
2 652 2016-07-01 2016-06-01 2016-05-01 2016-04-01 2016-03-01 2016-02-01
3 763 2014-09-01 2014-08-01 2014-07-01 2014-06-01 2014-05-01 2014-04-01
4 234 2018-12-01 2018-11-01 2018-10-01 2018-09-01 2018-08-01 2018-07-01






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 4 at 3:05









Wen-BenWen-Ben

124k83771




124k83771













  • Thank you sir, could you please explain a little bit about how to calculate 's'?

    – Long_NgV
    Jan 4 at 10:28



















  • Thank you sir, could you please explain a little bit about how to calculate 's'?

    – Long_NgV
    Jan 4 at 10:28

















Thank you sir, could you please explain a little bit about how to calculate 's'?

– Long_NgV
Jan 4 at 10:28





Thank you sir, could you please explain a little bit about how to calculate 's'?

– Long_NgV
Jan 4 at 10:28




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032552%2fcreate-multiple-columns-based-on-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas