Modify the column headers of a pandas data frame












1















I have this column header:



enter image description here



I put these into a list by x = list(df) and the type of x is a string. I want to put these into another list of strings but converting them into 8/18, 7/18,6/18, etc...



Here is the df.head() output:



enter image description here



Output of print(df.columns)



<class 'str'>
Index(['NDD 8/31', 'Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'May 2018(P&I Applied)',
'Apr 2018(P&I Applied)', 'Mar 2018(P&I Applied)',
'Feb 2018(P&I Expected)', 'Jan 2018(P&I Applied)',
'Dec 2017(P&I Applied)', 'Nov 2017(P&I Applied)',
'Oct 2017(P&I Applied)', 'Sep 2017(P&I Applied)',
'Paystring as of cut off as if 10.31',
'Paystrings as of


Does anyone know how to achieve this?










share|improve this question

























  • Can you add the output of print(df.columns) to the question?

    – Scott Boston
    Nov 15 '18 at 22:31











  • @ScottBoston Added

    – Snorrlaxxx
    Nov 15 '18 at 22:36











  • Do you want to replace Jun 2018 -> 6/18 only, or should the entire column name be 6/18?

    – Matthias Ossadnik
    Nov 15 '18 at 22:56











  • @MatthiasOssadnik No I do not need to modify the data frame, I just want to convert Jun 2018 - > 6/18 and put it into a vector or list

    – Snorrlaxxx
    Nov 15 '18 at 23:04
















1















I have this column header:



enter image description here



I put these into a list by x = list(df) and the type of x is a string. I want to put these into another list of strings but converting them into 8/18, 7/18,6/18, etc...



Here is the df.head() output:



enter image description here



Output of print(df.columns)



<class 'str'>
Index(['NDD 8/31', 'Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'May 2018(P&I Applied)',
'Apr 2018(P&I Applied)', 'Mar 2018(P&I Applied)',
'Feb 2018(P&I Expected)', 'Jan 2018(P&I Applied)',
'Dec 2017(P&I Applied)', 'Nov 2017(P&I Applied)',
'Oct 2017(P&I Applied)', 'Sep 2017(P&I Applied)',
'Paystring as of cut off as if 10.31',
'Paystrings as of


Does anyone know how to achieve this?










share|improve this question

























  • Can you add the output of print(df.columns) to the question?

    – Scott Boston
    Nov 15 '18 at 22:31











  • @ScottBoston Added

    – Snorrlaxxx
    Nov 15 '18 at 22:36











  • Do you want to replace Jun 2018 -> 6/18 only, or should the entire column name be 6/18?

    – Matthias Ossadnik
    Nov 15 '18 at 22:56











  • @MatthiasOssadnik No I do not need to modify the data frame, I just want to convert Jun 2018 - > 6/18 and put it into a vector or list

    – Snorrlaxxx
    Nov 15 '18 at 23:04














1












1








1








I have this column header:



enter image description here



I put these into a list by x = list(df) and the type of x is a string. I want to put these into another list of strings but converting them into 8/18, 7/18,6/18, etc...



Here is the df.head() output:



enter image description here



Output of print(df.columns)



<class 'str'>
Index(['NDD 8/31', 'Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'May 2018(P&I Applied)',
'Apr 2018(P&I Applied)', 'Mar 2018(P&I Applied)',
'Feb 2018(P&I Expected)', 'Jan 2018(P&I Applied)',
'Dec 2017(P&I Applied)', 'Nov 2017(P&I Applied)',
'Oct 2017(P&I Applied)', 'Sep 2017(P&I Applied)',
'Paystring as of cut off as if 10.31',
'Paystrings as of


Does anyone know how to achieve this?










share|improve this question
















I have this column header:



enter image description here



I put these into a list by x = list(df) and the type of x is a string. I want to put these into another list of strings but converting them into 8/18, 7/18,6/18, etc...



Here is the df.head() output:



enter image description here



Output of print(df.columns)



<class 'str'>
Index(['NDD 8/31', 'Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'May 2018(P&I Applied)',
'Apr 2018(P&I Applied)', 'Mar 2018(P&I Applied)',
'Feb 2018(P&I Expected)', 'Jan 2018(P&I Applied)',
'Dec 2017(P&I Applied)', 'Nov 2017(P&I Applied)',
'Oct 2017(P&I Applied)', 'Sep 2017(P&I Applied)',
'Paystring as of cut off as if 10.31',
'Paystrings as of


Does anyone know how to achieve this?







python pandas datetime dataframe indexing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 23:18









jpp

94.1k2155106




94.1k2155106










asked Nov 15 '18 at 22:24









SnorrlaxxxSnorrlaxxx

14511




14511













  • Can you add the output of print(df.columns) to the question?

    – Scott Boston
    Nov 15 '18 at 22:31











  • @ScottBoston Added

    – Snorrlaxxx
    Nov 15 '18 at 22:36











  • Do you want to replace Jun 2018 -> 6/18 only, or should the entire column name be 6/18?

    – Matthias Ossadnik
    Nov 15 '18 at 22:56











  • @MatthiasOssadnik No I do not need to modify the data frame, I just want to convert Jun 2018 - > 6/18 and put it into a vector or list

    – Snorrlaxxx
    Nov 15 '18 at 23:04



















  • Can you add the output of print(df.columns) to the question?

    – Scott Boston
    Nov 15 '18 at 22:31











  • @ScottBoston Added

    – Snorrlaxxx
    Nov 15 '18 at 22:36











  • Do you want to replace Jun 2018 -> 6/18 only, or should the entire column name be 6/18?

    – Matthias Ossadnik
    Nov 15 '18 at 22:56











  • @MatthiasOssadnik No I do not need to modify the data frame, I just want to convert Jun 2018 - > 6/18 and put it into a vector or list

    – Snorrlaxxx
    Nov 15 '18 at 23:04

















Can you add the output of print(df.columns) to the question?

– Scott Boston
Nov 15 '18 at 22:31





Can you add the output of print(df.columns) to the question?

– Scott Boston
Nov 15 '18 at 22:31













@ScottBoston Added

– Snorrlaxxx
Nov 15 '18 at 22:36





@ScottBoston Added

– Snorrlaxxx
Nov 15 '18 at 22:36













Do you want to replace Jun 2018 -> 6/18 only, or should the entire column name be 6/18?

– Matthias Ossadnik
Nov 15 '18 at 22:56





Do you want to replace Jun 2018 -> 6/18 only, or should the entire column name be 6/18?

– Matthias Ossadnik
Nov 15 '18 at 22:56













@MatthiasOssadnik No I do not need to modify the data frame, I just want to convert Jun 2018 - > 6/18 and put it into a vector or list

– Snorrlaxxx
Nov 15 '18 at 23:04





@MatthiasOssadnik No I do not need to modify the data frame, I just want to convert Jun 2018 - > 6/18 and put it into a vector or list

– Snorrlaxxx
Nov 15 '18 at 23:04












1 Answer
1






active

oldest

votes


















1














You can convert to datetime and then use strftime. The below logic also ensures non-date columns remain unchanged.



df = pd.DataFrame(columns=['Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'Paystring as of cut off as if 10.31'])

dates = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))

df.columns = dates.dt.strftime('%m/%y').mask(dates.isnull(), pd.Series(df.columns))

print(df.columns)

# Index(['08/18', '07/18', '06/18', 'Paystring as of cut off as if 10.31'], dtype='object')

print(dates.dt.month)

# 0 8.0
# 1 7.0
# 2 6.0
# 3 NaN
# dtype: float64





share|improve this answer


























  • Could you re-edit your answer to include the data frame I have?

    – Snorrlaxxx
    Nov 15 '18 at 23:21











  • @Snorrlaxxx, No I can't, because your question doesn't have a complete dataframe.

    – jpp
    Nov 15 '18 at 23:23













  • Okay, how would I convert the series into a string?

    – Snorrlaxxx
    Nov 15 '18 at 23:25











  • @Snorrlaxxx, I don't understand your question, maybe str(my_series) ?

    – jpp
    Nov 15 '18 at 23:25











  • Quick question how do I just get the months from dates list?

    – Snorrlaxxx
    Nov 15 '18 at 23:35











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%2f53328726%2fmodify-the-column-headers-of-a-pandas-data-frame%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









1














You can convert to datetime and then use strftime. The below logic also ensures non-date columns remain unchanged.



df = pd.DataFrame(columns=['Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'Paystring as of cut off as if 10.31'])

dates = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))

df.columns = dates.dt.strftime('%m/%y').mask(dates.isnull(), pd.Series(df.columns))

print(df.columns)

# Index(['08/18', '07/18', '06/18', 'Paystring as of cut off as if 10.31'], dtype='object')

print(dates.dt.month)

# 0 8.0
# 1 7.0
# 2 6.0
# 3 NaN
# dtype: float64





share|improve this answer


























  • Could you re-edit your answer to include the data frame I have?

    – Snorrlaxxx
    Nov 15 '18 at 23:21











  • @Snorrlaxxx, No I can't, because your question doesn't have a complete dataframe.

    – jpp
    Nov 15 '18 at 23:23













  • Okay, how would I convert the series into a string?

    – Snorrlaxxx
    Nov 15 '18 at 23:25











  • @Snorrlaxxx, I don't understand your question, maybe str(my_series) ?

    – jpp
    Nov 15 '18 at 23:25











  • Quick question how do I just get the months from dates list?

    – Snorrlaxxx
    Nov 15 '18 at 23:35
















1














You can convert to datetime and then use strftime. The below logic also ensures non-date columns remain unchanged.



df = pd.DataFrame(columns=['Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'Paystring as of cut off as if 10.31'])

dates = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))

df.columns = dates.dt.strftime('%m/%y').mask(dates.isnull(), pd.Series(df.columns))

print(df.columns)

# Index(['08/18', '07/18', '06/18', 'Paystring as of cut off as if 10.31'], dtype='object')

print(dates.dt.month)

# 0 8.0
# 1 7.0
# 2 6.0
# 3 NaN
# dtype: float64





share|improve this answer


























  • Could you re-edit your answer to include the data frame I have?

    – Snorrlaxxx
    Nov 15 '18 at 23:21











  • @Snorrlaxxx, No I can't, because your question doesn't have a complete dataframe.

    – jpp
    Nov 15 '18 at 23:23













  • Okay, how would I convert the series into a string?

    – Snorrlaxxx
    Nov 15 '18 at 23:25











  • @Snorrlaxxx, I don't understand your question, maybe str(my_series) ?

    – jpp
    Nov 15 '18 at 23:25











  • Quick question how do I just get the months from dates list?

    – Snorrlaxxx
    Nov 15 '18 at 23:35














1












1








1







You can convert to datetime and then use strftime. The below logic also ensures non-date columns remain unchanged.



df = pd.DataFrame(columns=['Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'Paystring as of cut off as if 10.31'])

dates = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))

df.columns = dates.dt.strftime('%m/%y').mask(dates.isnull(), pd.Series(df.columns))

print(df.columns)

# Index(['08/18', '07/18', '06/18', 'Paystring as of cut off as if 10.31'], dtype='object')

print(dates.dt.month)

# 0 8.0
# 1 7.0
# 2 6.0
# 3 NaN
# dtype: float64





share|improve this answer















You can convert to datetime and then use strftime. The below logic also ensures non-date columns remain unchanged.



df = pd.DataFrame(columns=['Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'Paystring as of cut off as if 10.31'])

dates = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))

df.columns = dates.dt.strftime('%m/%y').mask(dates.isnull(), pd.Series(df.columns))

print(df.columns)

# Index(['08/18', '07/18', '06/18', 'Paystring as of cut off as if 10.31'], dtype='object')

print(dates.dt.month)

# 0 8.0
# 1 7.0
# 2 6.0
# 3 NaN
# dtype: float64






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 23:45

























answered Nov 15 '18 at 22:32









jppjpp

94.1k2155106




94.1k2155106













  • Could you re-edit your answer to include the data frame I have?

    – Snorrlaxxx
    Nov 15 '18 at 23:21











  • @Snorrlaxxx, No I can't, because your question doesn't have a complete dataframe.

    – jpp
    Nov 15 '18 at 23:23













  • Okay, how would I convert the series into a string?

    – Snorrlaxxx
    Nov 15 '18 at 23:25











  • @Snorrlaxxx, I don't understand your question, maybe str(my_series) ?

    – jpp
    Nov 15 '18 at 23:25











  • Quick question how do I just get the months from dates list?

    – Snorrlaxxx
    Nov 15 '18 at 23:35



















  • Could you re-edit your answer to include the data frame I have?

    – Snorrlaxxx
    Nov 15 '18 at 23:21











  • @Snorrlaxxx, No I can't, because your question doesn't have a complete dataframe.

    – jpp
    Nov 15 '18 at 23:23













  • Okay, how would I convert the series into a string?

    – Snorrlaxxx
    Nov 15 '18 at 23:25











  • @Snorrlaxxx, I don't understand your question, maybe str(my_series) ?

    – jpp
    Nov 15 '18 at 23:25











  • Quick question how do I just get the months from dates list?

    – Snorrlaxxx
    Nov 15 '18 at 23:35

















Could you re-edit your answer to include the data frame I have?

– Snorrlaxxx
Nov 15 '18 at 23:21





Could you re-edit your answer to include the data frame I have?

– Snorrlaxxx
Nov 15 '18 at 23:21













@Snorrlaxxx, No I can't, because your question doesn't have a complete dataframe.

– jpp
Nov 15 '18 at 23:23







@Snorrlaxxx, No I can't, because your question doesn't have a complete dataframe.

– jpp
Nov 15 '18 at 23:23















Okay, how would I convert the series into a string?

– Snorrlaxxx
Nov 15 '18 at 23:25





Okay, how would I convert the series into a string?

– Snorrlaxxx
Nov 15 '18 at 23:25













@Snorrlaxxx, I don't understand your question, maybe str(my_series) ?

– jpp
Nov 15 '18 at 23:25





@Snorrlaxxx, I don't understand your question, maybe str(my_series) ?

– jpp
Nov 15 '18 at 23:25













Quick question how do I just get the months from dates list?

– Snorrlaxxx
Nov 15 '18 at 23:35





Quick question how do I just get the months from dates list?

– Snorrlaxxx
Nov 15 '18 at 23:35


















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%2f53328726%2fmodify-the-column-headers-of-a-pandas-data-frame%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

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)