Return first numeric value in a column












1















This is pretty much what my dataframe looks like (indexed by year and countries.)



                   ISO   gini  efw
year countries
1970 Argentina ARG NaN 5.67
1975 Argentina ARG NaN 3.13
1980 Argentina ARG 40.8 4.25
1985 Argentina ARG NaN 3.53
1990 Argentina ARG NaN 4.47
1970 Bolivia BOL NaN NaN
1975 Bolivia BOL NaN NaN
1980 Bolivia BOL NaN 4.08
1985 Bolivia BOL NaN 3.52
1990 Bolivia BOL 42.0 5.62
2010 Uruguay URY 44.5 7.33
2011 Uruguay URY 42.2 7.39
2012 Uruguay URY 39.9 7.34
2013 Uruguay URY 40.5 7.26
1970 Venezuela VEN NaN 7.18
1975 Venezuela VEN NaN 6.22
1980 Venezuela VEN NaN 6.72
1985 Venezuela VEN NaN 6.08
1990 Venezuela VEN NaN 5.55
1995 Venezuela VEN 47.8 4.35
2000 Venezuela VEN NaN 5.89


I want to come up with a function that identifies the first non NaN value in the gini column, and returns the year and efw value that correspond to the value in the gini column.



For example, if the first non Nan for Argentina is 40.8, I want the function to return 40.8, the year for that value (1980), and the value for efw also for 1980 (4.25).



Ideally the new dataframe would look like this. That for every country.



                    ISO   gini  efw
year countries
1980 Argentina ARG 40.8 4.25
2016 Argentina ARG 43.60 3.13


The last row corresponds to 2016, the last year for which there is data available.










share|improve this question




















  • 1





    Can you check your desired output df there? I don't see where you got the second row from... First makes sense, second I don't see here. Wouldn't the next row be 1990 Bolivia BOL 32.0 5.62?

    – Capn Jack
    Nov 16 '18 at 3:45






  • 1





    Is there any particular reason why year and countries are indexes? I recommend those to be columns as those are more flexible to work with.

    – vlizana
    Nov 16 '18 at 3:54
















1















This is pretty much what my dataframe looks like (indexed by year and countries.)



                   ISO   gini  efw
year countries
1970 Argentina ARG NaN 5.67
1975 Argentina ARG NaN 3.13
1980 Argentina ARG 40.8 4.25
1985 Argentina ARG NaN 3.53
1990 Argentina ARG NaN 4.47
1970 Bolivia BOL NaN NaN
1975 Bolivia BOL NaN NaN
1980 Bolivia BOL NaN 4.08
1985 Bolivia BOL NaN 3.52
1990 Bolivia BOL 42.0 5.62
2010 Uruguay URY 44.5 7.33
2011 Uruguay URY 42.2 7.39
2012 Uruguay URY 39.9 7.34
2013 Uruguay URY 40.5 7.26
1970 Venezuela VEN NaN 7.18
1975 Venezuela VEN NaN 6.22
1980 Venezuela VEN NaN 6.72
1985 Venezuela VEN NaN 6.08
1990 Venezuela VEN NaN 5.55
1995 Venezuela VEN 47.8 4.35
2000 Venezuela VEN NaN 5.89


I want to come up with a function that identifies the first non NaN value in the gini column, and returns the year and efw value that correspond to the value in the gini column.



For example, if the first non Nan for Argentina is 40.8, I want the function to return 40.8, the year for that value (1980), and the value for efw also for 1980 (4.25).



Ideally the new dataframe would look like this. That for every country.



                    ISO   gini  efw
year countries
1980 Argentina ARG 40.8 4.25
2016 Argentina ARG 43.60 3.13


The last row corresponds to 2016, the last year for which there is data available.










share|improve this question




















  • 1





    Can you check your desired output df there? I don't see where you got the second row from... First makes sense, second I don't see here. Wouldn't the next row be 1990 Bolivia BOL 32.0 5.62?

    – Capn Jack
    Nov 16 '18 at 3:45






  • 1





    Is there any particular reason why year and countries are indexes? I recommend those to be columns as those are more flexible to work with.

    – vlizana
    Nov 16 '18 at 3:54














1












1








1








This is pretty much what my dataframe looks like (indexed by year and countries.)



                   ISO   gini  efw
year countries
1970 Argentina ARG NaN 5.67
1975 Argentina ARG NaN 3.13
1980 Argentina ARG 40.8 4.25
1985 Argentina ARG NaN 3.53
1990 Argentina ARG NaN 4.47
1970 Bolivia BOL NaN NaN
1975 Bolivia BOL NaN NaN
1980 Bolivia BOL NaN 4.08
1985 Bolivia BOL NaN 3.52
1990 Bolivia BOL 42.0 5.62
2010 Uruguay URY 44.5 7.33
2011 Uruguay URY 42.2 7.39
2012 Uruguay URY 39.9 7.34
2013 Uruguay URY 40.5 7.26
1970 Venezuela VEN NaN 7.18
1975 Venezuela VEN NaN 6.22
1980 Venezuela VEN NaN 6.72
1985 Venezuela VEN NaN 6.08
1990 Venezuela VEN NaN 5.55
1995 Venezuela VEN 47.8 4.35
2000 Venezuela VEN NaN 5.89


I want to come up with a function that identifies the first non NaN value in the gini column, and returns the year and efw value that correspond to the value in the gini column.



For example, if the first non Nan for Argentina is 40.8, I want the function to return 40.8, the year for that value (1980), and the value for efw also for 1980 (4.25).



Ideally the new dataframe would look like this. That for every country.



                    ISO   gini  efw
year countries
1980 Argentina ARG 40.8 4.25
2016 Argentina ARG 43.60 3.13


The last row corresponds to 2016, the last year for which there is data available.










share|improve this question
















This is pretty much what my dataframe looks like (indexed by year and countries.)



                   ISO   gini  efw
year countries
1970 Argentina ARG NaN 5.67
1975 Argentina ARG NaN 3.13
1980 Argentina ARG 40.8 4.25
1985 Argentina ARG NaN 3.53
1990 Argentina ARG NaN 4.47
1970 Bolivia BOL NaN NaN
1975 Bolivia BOL NaN NaN
1980 Bolivia BOL NaN 4.08
1985 Bolivia BOL NaN 3.52
1990 Bolivia BOL 42.0 5.62
2010 Uruguay URY 44.5 7.33
2011 Uruguay URY 42.2 7.39
2012 Uruguay URY 39.9 7.34
2013 Uruguay URY 40.5 7.26
1970 Venezuela VEN NaN 7.18
1975 Venezuela VEN NaN 6.22
1980 Venezuela VEN NaN 6.72
1985 Venezuela VEN NaN 6.08
1990 Venezuela VEN NaN 5.55
1995 Venezuela VEN 47.8 4.35
2000 Venezuela VEN NaN 5.89


I want to come up with a function that identifies the first non NaN value in the gini column, and returns the year and efw value that correspond to the value in the gini column.



For example, if the first non Nan for Argentina is 40.8, I want the function to return 40.8, the year for that value (1980), and the value for efw also for 1980 (4.25).



Ideally the new dataframe would look like this. That for every country.



                    ISO   gini  efw
year countries
1980 Argentina ARG 40.8 4.25
2016 Argentina ARG 43.60 3.13


The last row corresponds to 2016, the last year for which there is data available.







python pandas function dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 3:47







Guillermina Sutter Schneider

















asked Nov 16 '18 at 3:42









Guillermina Sutter SchneiderGuillermina Sutter Schneider

12011




12011








  • 1





    Can you check your desired output df there? I don't see where you got the second row from... First makes sense, second I don't see here. Wouldn't the next row be 1990 Bolivia BOL 32.0 5.62?

    – Capn Jack
    Nov 16 '18 at 3:45






  • 1





    Is there any particular reason why year and countries are indexes? I recommend those to be columns as those are more flexible to work with.

    – vlizana
    Nov 16 '18 at 3:54














  • 1





    Can you check your desired output df there? I don't see where you got the second row from... First makes sense, second I don't see here. Wouldn't the next row be 1990 Bolivia BOL 32.0 5.62?

    – Capn Jack
    Nov 16 '18 at 3:45






  • 1





    Is there any particular reason why year and countries are indexes? I recommend those to be columns as those are more flexible to work with.

    – vlizana
    Nov 16 '18 at 3:54








1




1





Can you check your desired output df there? I don't see where you got the second row from... First makes sense, second I don't see here. Wouldn't the next row be 1990 Bolivia BOL 32.0 5.62?

– Capn Jack
Nov 16 '18 at 3:45





Can you check your desired output df there? I don't see where you got the second row from... First makes sense, second I don't see here. Wouldn't the next row be 1990 Bolivia BOL 32.0 5.62?

– Capn Jack
Nov 16 '18 at 3:45




1




1





Is there any particular reason why year and countries are indexes? I recommend those to be columns as those are more flexible to work with.

– vlizana
Nov 16 '18 at 3:54





Is there any particular reason why year and countries are indexes? I recommend those to be columns as those are more flexible to work with.

– vlizana
Nov 16 '18 at 3:54












1 Answer
1






active

oldest

votes


















1














Use this, get by condition, then reset the index, then sort the values, then multiindex the dataframe again:



print(df[df['gini'].notnull()].reset_index().sort_values('year').iloc[[0, -1]].set_index(['year','countries']))





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%2f53331096%2freturn-first-numeric-value-in-a-column%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














    Use this, get by condition, then reset the index, then sort the values, then multiindex the dataframe again:



    print(df[df['gini'].notnull()].reset_index().sort_values('year').iloc[[0, -1]].set_index(['year','countries']))





    share|improve this answer




























      1














      Use this, get by condition, then reset the index, then sort the values, then multiindex the dataframe again:



      print(df[df['gini'].notnull()].reset_index().sort_values('year').iloc[[0, -1]].set_index(['year','countries']))





      share|improve this answer


























        1












        1








        1







        Use this, get by condition, then reset the index, then sort the values, then multiindex the dataframe again:



        print(df[df['gini'].notnull()].reset_index().sort_values('year').iloc[[0, -1]].set_index(['year','countries']))





        share|improve this answer













        Use this, get by condition, then reset the index, then sort the values, then multiindex the dataframe again:



        print(df[df['gini'].notnull()].reset_index().sort_values('year').iloc[[0, -1]].set_index(['year','countries']))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 3:59









        U9-ForwardU9-Forward

        16.9k51643




        16.9k51643
































            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%2f53331096%2freturn-first-numeric-value-in-a-column%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python