Remove string from dataframe index values












0















I want to remove strings from my index values:



df.index.get_values().str.replace("and over", "").astype(int)


Doing this returns the following error:



AttributeError: 'numpy.ndarray' object has no attribute 'str'


I've tried to find a similar function used by numpy to achieve this but I can't seem to find any. Here are my index values:



['0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15'
'16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29'
'30' '31' '32' '33' '34' '35' '36' '37' '38' '39' '40' '41' '42' '43'
'44' '45' '46' '47' '48' '49' '50' '51' '52' '53' '54' '55' '56' '57'
'58' '59' '60' '61' '62' '63' '64' '65' '66' '67' '68' '69' '70' '71'
'72' '73' '74' '75' '76' '77' '78' '79' '80' '81' '82' '83' '84' '85'
'86' '87' '88' '89' '90 and over' 'All ages']









share|improve this question























  • Move the method get_values after the replace such as df.index.str.replace("and over", "").get_values() should not throw an error. But the astype might not work as you also have 'All ages' that can't be change to int

    – Ben.T
    Nov 20 '18 at 19:53













  • @Ben.T If the All ages string was replaced to be an empty string would it be possible to loop through the index changing each value to an integer?

    – Rotav
    Nov 20 '18 at 20:26













  • Don't think so, you can try to replace by -1 and it should work. If you explain more why you want to change the type and the final application, it may bring other solution

    – Ben.T
    Nov 20 '18 at 20:46











  • @Ben.T The dataframe contains the population of an area broken down by age. Using the data, I am creating a line graph with matplotlib to show how the population (y axis) changes for the area by age (x axis). Because there are so many index values, they overlap on the x axis. To solve this I thought it may help if the index was recognised by the file as numerical rather than as strings, which would remove the overlap automatically. I'll also be using the data to produce other graphs and thus wanted the problem to be solved with one solution.

    – Rotav
    Nov 20 '18 at 21:37
















0















I want to remove strings from my index values:



df.index.get_values().str.replace("and over", "").astype(int)


Doing this returns the following error:



AttributeError: 'numpy.ndarray' object has no attribute 'str'


I've tried to find a similar function used by numpy to achieve this but I can't seem to find any. Here are my index values:



['0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15'
'16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29'
'30' '31' '32' '33' '34' '35' '36' '37' '38' '39' '40' '41' '42' '43'
'44' '45' '46' '47' '48' '49' '50' '51' '52' '53' '54' '55' '56' '57'
'58' '59' '60' '61' '62' '63' '64' '65' '66' '67' '68' '69' '70' '71'
'72' '73' '74' '75' '76' '77' '78' '79' '80' '81' '82' '83' '84' '85'
'86' '87' '88' '89' '90 and over' 'All ages']









share|improve this question























  • Move the method get_values after the replace such as df.index.str.replace("and over", "").get_values() should not throw an error. But the astype might not work as you also have 'All ages' that can't be change to int

    – Ben.T
    Nov 20 '18 at 19:53













  • @Ben.T If the All ages string was replaced to be an empty string would it be possible to loop through the index changing each value to an integer?

    – Rotav
    Nov 20 '18 at 20:26













  • Don't think so, you can try to replace by -1 and it should work. If you explain more why you want to change the type and the final application, it may bring other solution

    – Ben.T
    Nov 20 '18 at 20:46











  • @Ben.T The dataframe contains the population of an area broken down by age. Using the data, I am creating a line graph with matplotlib to show how the population (y axis) changes for the area by age (x axis). Because there are so many index values, they overlap on the x axis. To solve this I thought it may help if the index was recognised by the file as numerical rather than as strings, which would remove the overlap automatically. I'll also be using the data to produce other graphs and thus wanted the problem to be solved with one solution.

    – Rotav
    Nov 20 '18 at 21:37














0












0








0








I want to remove strings from my index values:



df.index.get_values().str.replace("and over", "").astype(int)


Doing this returns the following error:



AttributeError: 'numpy.ndarray' object has no attribute 'str'


I've tried to find a similar function used by numpy to achieve this but I can't seem to find any. Here are my index values:



['0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15'
'16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29'
'30' '31' '32' '33' '34' '35' '36' '37' '38' '39' '40' '41' '42' '43'
'44' '45' '46' '47' '48' '49' '50' '51' '52' '53' '54' '55' '56' '57'
'58' '59' '60' '61' '62' '63' '64' '65' '66' '67' '68' '69' '70' '71'
'72' '73' '74' '75' '76' '77' '78' '79' '80' '81' '82' '83' '84' '85'
'86' '87' '88' '89' '90 and over' 'All ages']









share|improve this question














I want to remove strings from my index values:



df.index.get_values().str.replace("and over", "").astype(int)


Doing this returns the following error:



AttributeError: 'numpy.ndarray' object has no attribute 'str'


I've tried to find a similar function used by numpy to achieve this but I can't seem to find any. Here are my index values:



['0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15'
'16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29'
'30' '31' '32' '33' '34' '35' '36' '37' '38' '39' '40' '41' '42' '43'
'44' '45' '46' '47' '48' '49' '50' '51' '52' '53' '54' '55' '56' '57'
'58' '59' '60' '61' '62' '63' '64' '65' '66' '67' '68' '69' '70' '71'
'72' '73' '74' '75' '76' '77' '78' '79' '80' '81' '82' '83' '84' '85'
'86' '87' '88' '89' '90 and over' 'All ages']






numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 18:45









RotavRotav

284




284













  • Move the method get_values after the replace such as df.index.str.replace("and over", "").get_values() should not throw an error. But the astype might not work as you also have 'All ages' that can't be change to int

    – Ben.T
    Nov 20 '18 at 19:53













  • @Ben.T If the All ages string was replaced to be an empty string would it be possible to loop through the index changing each value to an integer?

    – Rotav
    Nov 20 '18 at 20:26













  • Don't think so, you can try to replace by -1 and it should work. If you explain more why you want to change the type and the final application, it may bring other solution

    – Ben.T
    Nov 20 '18 at 20:46











  • @Ben.T The dataframe contains the population of an area broken down by age. Using the data, I am creating a line graph with matplotlib to show how the population (y axis) changes for the area by age (x axis). Because there are so many index values, they overlap on the x axis. To solve this I thought it may help if the index was recognised by the file as numerical rather than as strings, which would remove the overlap automatically. I'll also be using the data to produce other graphs and thus wanted the problem to be solved with one solution.

    – Rotav
    Nov 20 '18 at 21:37



















  • Move the method get_values after the replace such as df.index.str.replace("and over", "").get_values() should not throw an error. But the astype might not work as you also have 'All ages' that can't be change to int

    – Ben.T
    Nov 20 '18 at 19:53













  • @Ben.T If the All ages string was replaced to be an empty string would it be possible to loop through the index changing each value to an integer?

    – Rotav
    Nov 20 '18 at 20:26













  • Don't think so, you can try to replace by -1 and it should work. If you explain more why you want to change the type and the final application, it may bring other solution

    – Ben.T
    Nov 20 '18 at 20:46











  • @Ben.T The dataframe contains the population of an area broken down by age. Using the data, I am creating a line graph with matplotlib to show how the population (y axis) changes for the area by age (x axis). Because there are so many index values, they overlap on the x axis. To solve this I thought it may help if the index was recognised by the file as numerical rather than as strings, which would remove the overlap automatically. I'll also be using the data to produce other graphs and thus wanted the problem to be solved with one solution.

    – Rotav
    Nov 20 '18 at 21:37

















Move the method get_values after the replace such as df.index.str.replace("and over", "").get_values() should not throw an error. But the astype might not work as you also have 'All ages' that can't be change to int

– Ben.T
Nov 20 '18 at 19:53







Move the method get_values after the replace such as df.index.str.replace("and over", "").get_values() should not throw an error. But the astype might not work as you also have 'All ages' that can't be change to int

– Ben.T
Nov 20 '18 at 19:53















@Ben.T If the All ages string was replaced to be an empty string would it be possible to loop through the index changing each value to an integer?

– Rotav
Nov 20 '18 at 20:26







@Ben.T If the All ages string was replaced to be an empty string would it be possible to loop through the index changing each value to an integer?

– Rotav
Nov 20 '18 at 20:26















Don't think so, you can try to replace by -1 and it should work. If you explain more why you want to change the type and the final application, it may bring other solution

– Ben.T
Nov 20 '18 at 20:46





Don't think so, you can try to replace by -1 and it should work. If you explain more why you want to change the type and the final application, it may bring other solution

– Ben.T
Nov 20 '18 at 20:46













@Ben.T The dataframe contains the population of an area broken down by age. Using the data, I am creating a line graph with matplotlib to show how the population (y axis) changes for the area by age (x axis). Because there are so many index values, they overlap on the x axis. To solve this I thought it may help if the index was recognised by the file as numerical rather than as strings, which would remove the overlap automatically. I'll also be using the data to produce other graphs and thus wanted the problem to be solved with one solution.

– Rotav
Nov 20 '18 at 21:37





@Ben.T The dataframe contains the population of an area broken down by age. Using the data, I am creating a line graph with matplotlib to show how the population (y axis) changes for the area by age (x axis). Because there are so many index values, they overlap on the x axis. To solve this I thought it may help if the index was recognised by the file as numerical rather than as strings, which would remove the overlap automatically. I'll also be using the data to produce other graphs and thus wanted the problem to be solved with one solution.

– Rotav
Nov 20 '18 at 21:37












1 Answer
1






active

oldest

votes


















0














Something like the following code should work. But first, you would have to delete the 'All ages' entry.



arr = np.array([x.replace(' and over', '') for x in arr]).astype(int)





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%2f53399550%2fremove-string-from-dataframe-index-values%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














    Something like the following code should work. But first, you would have to delete the 'All ages' entry.



    arr = np.array([x.replace(' and over', '') for x in arr]).astype(int)





    share|improve this answer






























      0














      Something like the following code should work. But first, you would have to delete the 'All ages' entry.



      arr = np.array([x.replace(' and over', '') for x in arr]).astype(int)





      share|improve this answer




























        0












        0








        0







        Something like the following code should work. But first, you would have to delete the 'All ages' entry.



        arr = np.array([x.replace(' and over', '') for x in arr]).astype(int)





        share|improve this answer















        Something like the following code should work. But first, you would have to delete the 'All ages' entry.



        arr = np.array([x.replace(' and over', '') for x in arr]).astype(int)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 3:09









        Pang

        6,8911563101




        6,8911563101










        answered Nov 20 '18 at 21:05









        Esteban QuirosEsteban Quiros

        1015




        1015






























            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%2f53399550%2fremove-string-from-dataframe-index-values%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?