Data Formatting pandas





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







0















I am trying to enter a line of code that creates a row for the index 31'st January 1995. I am unable to get the row to look like 31/01/1995 and instead the output is 1995-01-31 00:00:00 .



My original data in a dataframe called MainData



I am trying to add a row at the top for 31st January 1995 in the same format as the data below.



enter image description here



My code is



MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData


enter image description here



Please let me know if there is a way to reformat this to 31/01/1995.



Thanks in advance.










share|improve this question

























  • Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?

    – AnalysisNerd
    Nov 22 '18 at 20:33











  • I have no idea. Please make an MCVE

    – roganjosh
    Nov 22 '18 at 20:34













  • MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.

    – AnalysisNerd
    Nov 22 '18 at 20:34











  • I think it's better write a function that convert datetime type into string and then apply it on your column.

    – Ali AzG
    Nov 22 '18 at 20:35






  • 1





    Am editing my query to provide further information

    – AnalysisNerd
    Nov 22 '18 at 20:36


















0















I am trying to enter a line of code that creates a row for the index 31'st January 1995. I am unable to get the row to look like 31/01/1995 and instead the output is 1995-01-31 00:00:00 .



My original data in a dataframe called MainData



I am trying to add a row at the top for 31st January 1995 in the same format as the data below.



enter image description here



My code is



MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData


enter image description here



Please let me know if there is a way to reformat this to 31/01/1995.



Thanks in advance.










share|improve this question

























  • Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?

    – AnalysisNerd
    Nov 22 '18 at 20:33











  • I have no idea. Please make an MCVE

    – roganjosh
    Nov 22 '18 at 20:34













  • MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.

    – AnalysisNerd
    Nov 22 '18 at 20:34











  • I think it's better write a function that convert datetime type into string and then apply it on your column.

    – Ali AzG
    Nov 22 '18 at 20:35






  • 1





    Am editing my query to provide further information

    – AnalysisNerd
    Nov 22 '18 at 20:36














0












0








0


1






I am trying to enter a line of code that creates a row for the index 31'st January 1995. I am unable to get the row to look like 31/01/1995 and instead the output is 1995-01-31 00:00:00 .



My original data in a dataframe called MainData



I am trying to add a row at the top for 31st January 1995 in the same format as the data below.



enter image description here



My code is



MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData


enter image description here



Please let me know if there is a way to reformat this to 31/01/1995.



Thanks in advance.










share|improve this question
















I am trying to enter a line of code that creates a row for the index 31'st January 1995. I am unable to get the row to look like 31/01/1995 and instead the output is 1995-01-31 00:00:00 .



My original data in a dataframe called MainData



I am trying to add a row at the top for 31st January 1995 in the same format as the data below.



enter image description here



My code is



MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData


enter image description here



Please let me know if there is a way to reformat this to 31/01/1995.



Thanks in advance.







python pandas datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 20:39







AnalysisNerd

















asked Nov 22 '18 at 20:28









AnalysisNerdAnalysisNerd

69111




69111













  • Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?

    – AnalysisNerd
    Nov 22 '18 at 20:33











  • I have no idea. Please make an MCVE

    – roganjosh
    Nov 22 '18 at 20:34













  • MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.

    – AnalysisNerd
    Nov 22 '18 at 20:34











  • I think it's better write a function that convert datetime type into string and then apply it on your column.

    – Ali AzG
    Nov 22 '18 at 20:35






  • 1





    Am editing my query to provide further information

    – AnalysisNerd
    Nov 22 '18 at 20:36



















  • Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?

    – AnalysisNerd
    Nov 22 '18 at 20:33











  • I have no idea. Please make an MCVE

    – roganjosh
    Nov 22 '18 at 20:34













  • MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.

    – AnalysisNerd
    Nov 22 '18 at 20:34











  • I think it's better write a function that convert datetime type into string and then apply it on your column.

    – Ali AzG
    Nov 22 '18 at 20:35






  • 1





    Am editing my query to provide further information

    – AnalysisNerd
    Nov 22 '18 at 20:36

















Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?

– AnalysisNerd
Nov 22 '18 at 20:33





Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?

– AnalysisNerd
Nov 22 '18 at 20:33













I have no idea. Please make an MCVE

– roganjosh
Nov 22 '18 at 20:34







I have no idea. Please make an MCVE

– roganjosh
Nov 22 '18 at 20:34















MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.

– AnalysisNerd
Nov 22 '18 at 20:34





MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.

– AnalysisNerd
Nov 22 '18 at 20:34













I think it's better write a function that convert datetime type into string and then apply it on your column.

– Ali AzG
Nov 22 '18 at 20:35





I think it's better write a function that convert datetime type into string and then apply it on your column.

– Ali AzG
Nov 22 '18 at 20:35




1




1





Am editing my query to provide further information

– AnalysisNerd
Nov 22 '18 at 20:36





Am editing my query to provide further information

– AnalysisNerd
Nov 22 '18 at 20:36












1 Answer
1






active

oldest

votes


















0














#Making the data look more normal by removing the first column index level
MainData = MainData.rename(columns=MainData.iloc[0])
MainData = MainData.iloc[1:]
#Re-adjusting the Index to a datetime format
MainData['DateAdjusted'] = MainData.index
MainData = MainData.reset_index(drop=True)
MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)

#Just renaming the Column and converting the index back to Date
MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
MainData.index = MainData['Date']
del MainData['Date']

#Defining the date for the row I want to add
InitialDate = "31/01/1995"
format_str = '%d/%m/%Y'
datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
print (datetime_obj.date())

MainData.loc[datetime_obj,:] = [100 for number in range(7)]
MainData = MainData.sort_index(ascending=True)





share|improve this answer
























    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%2f53437655%2fdata-formatting-pandas%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









    0














    #Making the data look more normal by removing the first column index level
    MainData = MainData.rename(columns=MainData.iloc[0])
    MainData = MainData.iloc[1:]
    #Re-adjusting the Index to a datetime format
    MainData['DateAdjusted'] = MainData.index
    MainData = MainData.reset_index(drop=True)
    MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)

    #Just renaming the Column and converting the index back to Date
    MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
    MainData.index = MainData['Date']
    del MainData['Date']

    #Defining the date for the row I want to add
    InitialDate = "31/01/1995"
    format_str = '%d/%m/%Y'
    datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
    print (datetime_obj.date())

    MainData.loc[datetime_obj,:] = [100 for number in range(7)]
    MainData = MainData.sort_index(ascending=True)





    share|improve this answer




























      0














      #Making the data look more normal by removing the first column index level
      MainData = MainData.rename(columns=MainData.iloc[0])
      MainData = MainData.iloc[1:]
      #Re-adjusting the Index to a datetime format
      MainData['DateAdjusted'] = MainData.index
      MainData = MainData.reset_index(drop=True)
      MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)

      #Just renaming the Column and converting the index back to Date
      MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
      MainData.index = MainData['Date']
      del MainData['Date']

      #Defining the date for the row I want to add
      InitialDate = "31/01/1995"
      format_str = '%d/%m/%Y'
      datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
      print (datetime_obj.date())

      MainData.loc[datetime_obj,:] = [100 for number in range(7)]
      MainData = MainData.sort_index(ascending=True)





      share|improve this answer


























        0












        0








        0







        #Making the data look more normal by removing the first column index level
        MainData = MainData.rename(columns=MainData.iloc[0])
        MainData = MainData.iloc[1:]
        #Re-adjusting the Index to a datetime format
        MainData['DateAdjusted'] = MainData.index
        MainData = MainData.reset_index(drop=True)
        MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)

        #Just renaming the Column and converting the index back to Date
        MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
        MainData.index = MainData['Date']
        del MainData['Date']

        #Defining the date for the row I want to add
        InitialDate = "31/01/1995"
        format_str = '%d/%m/%Y'
        datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
        print (datetime_obj.date())

        MainData.loc[datetime_obj,:] = [100 for number in range(7)]
        MainData = MainData.sort_index(ascending=True)





        share|improve this answer













        #Making the data look more normal by removing the first column index level
        MainData = MainData.rename(columns=MainData.iloc[0])
        MainData = MainData.iloc[1:]
        #Re-adjusting the Index to a datetime format
        MainData['DateAdjusted'] = MainData.index
        MainData = MainData.reset_index(drop=True)
        MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)

        #Just renaming the Column and converting the index back to Date
        MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
        MainData.index = MainData['Date']
        del MainData['Date']

        #Defining the date for the row I want to add
        InitialDate = "31/01/1995"
        format_str = '%d/%m/%Y'
        datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
        print (datetime_obj.date())

        MainData.loc[datetime_obj,:] = [100 for number in range(7)]
        MainData = MainData.sort_index(ascending=True)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 21:50









        AnalysisNerdAnalysisNerd

        69111




        69111
































            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%2f53437655%2fdata-formatting-pandas%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

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?