Get Row and Column with Minimum value in Entire Pandas DataFrame










3















The problem is simple and so must be solution but I am not able to find it.



I want to find which row and column in Pandas DataFrame has minimum value and how much is it.



I have tried following code (in addition to various combinations):



df = pd.DataFrame(data=[[4,5,6],[2,1,3],[7,0,5],[2,5,3]], 
index = ['R1','R2','R3','R4'],
columns=['C1','C2','C3'])

print(df)

print(df.loc[df.idxmin(axis=0), df.idxmin(axis=1)])


The dataframe (df) being searched is:



 C1 C2 C3
R1 4 5 6
R2 2 1 3
R3 7 0 5
R4 2 5 3


Output for the loc command:



 C1 C2 C2 C1
R2 2 1 1 2
R3 7 0 0 7
R2 2 1 1 2


What I need is:



 C2
R3 0


How can I get this simple result?










share|improve this question






















  • Performance is important?

    – jezrael
    Nov 14 '18 at 6:35











  • Working with some missing values is most important. Then display and then performance.

    – rnso
    Nov 14 '18 at 6:46
















3















The problem is simple and so must be solution but I am not able to find it.



I want to find which row and column in Pandas DataFrame has minimum value and how much is it.



I have tried following code (in addition to various combinations):



df = pd.DataFrame(data=[[4,5,6],[2,1,3],[7,0,5],[2,5,3]], 
index = ['R1','R2','R3','R4'],
columns=['C1','C2','C3'])

print(df)

print(df.loc[df.idxmin(axis=0), df.idxmin(axis=1)])


The dataframe (df) being searched is:



 C1 C2 C3
R1 4 5 6
R2 2 1 3
R3 7 0 5
R4 2 5 3


Output for the loc command:



 C1 C2 C2 C1
R2 2 1 1 2
R3 7 0 0 7
R2 2 1 1 2


What I need is:



 C2
R3 0


How can I get this simple result?










share|improve this question






















  • Performance is important?

    – jezrael
    Nov 14 '18 at 6:35











  • Working with some missing values is most important. Then display and then performance.

    – rnso
    Nov 14 '18 at 6:46














3












3








3








The problem is simple and so must be solution but I am not able to find it.



I want to find which row and column in Pandas DataFrame has minimum value and how much is it.



I have tried following code (in addition to various combinations):



df = pd.DataFrame(data=[[4,5,6],[2,1,3],[7,0,5],[2,5,3]], 
index = ['R1','R2','R3','R4'],
columns=['C1','C2','C3'])

print(df)

print(df.loc[df.idxmin(axis=0), df.idxmin(axis=1)])


The dataframe (df) being searched is:



 C1 C2 C3
R1 4 5 6
R2 2 1 3
R3 7 0 5
R4 2 5 3


Output for the loc command:



 C1 C2 C2 C1
R2 2 1 1 2
R3 7 0 0 7
R2 2 1 1 2


What I need is:



 C2
R3 0


How can I get this simple result?










share|improve this question














The problem is simple and so must be solution but I am not able to find it.



I want to find which row and column in Pandas DataFrame has minimum value and how much is it.



I have tried following code (in addition to various combinations):



df = pd.DataFrame(data=[[4,5,6],[2,1,3],[7,0,5],[2,5,3]], 
index = ['R1','R2','R3','R4'],
columns=['C1','C2','C3'])

print(df)

print(df.loc[df.idxmin(axis=0), df.idxmin(axis=1)])


The dataframe (df) being searched is:



 C1 C2 C3
R1 4 5 6
R2 2 1 3
R3 7 0 5
R4 2 5 3


Output for the loc command:



 C1 C2 C2 C1
R2 2 1 1 2
R3 7 0 0 7
R2 2 1 1 2


What I need is:



 C2
R3 0


How can I get this simple result?







python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 6:10









rnsornso

11.7k134196




11.7k134196












  • Performance is important?

    – jezrael
    Nov 14 '18 at 6:35











  • Working with some missing values is most important. Then display and then performance.

    – rnso
    Nov 14 '18 at 6:46


















  • Performance is important?

    – jezrael
    Nov 14 '18 at 6:35











  • Working with some missing values is most important. Then display and then performance.

    – rnso
    Nov 14 '18 at 6:46

















Performance is important?

– jezrael
Nov 14 '18 at 6:35





Performance is important?

– jezrael
Nov 14 '18 at 6:35













Working with some missing values is most important. Then display and then performance.

– rnso
Nov 14 '18 at 6:46






Working with some missing values is most important. Then display and then performance.

– rnso
Nov 14 '18 at 6:46













3 Answers
3






active

oldest

votes


















2














Use:



a, b = df.stack().idxmin()
print(df.loc[[a], [b]])
C2
R3 0


Another @John Zwinck solution working with missing values - use numpy.nanargmin:



df = pd.DataFrame(data=[[4,5,6],[2,np.nan,3],[7,0,5],[2,5,3]], 
index = ['R1','R2','R3','R4'],
columns=['C1','C2','C3'])

print(df)
C1 C2 C3
R1 4 5.0 6
R2 2 NaN 3
R3 7 0.0 5
R4 2 5.0 3

#https://stackoverflow.com/a/3230123
ri, ci = np.unravel_index(np.nanargmin(df.values), df.shape)
print(df.iloc[[ri], [ci]])
C2
R3 0.0





share|improve this answer

























  • Great. Forgot to add in question: there are some np.nan values in real df. Will this code work there as well?

    – rnso
    Nov 14 '18 at 6:17











  • @rnso - sure, pandas function working with nans nice.

    – jezrael
    Nov 14 '18 at 6:17











  • @rnso - changed solution for working with missing values.

    – jezrael
    Nov 14 '18 at 6:52


















1














I'd get the index this way:



np.unravel_index(np.argmin(df.values), df.shape)


This is much faster than df.stack().idxmin().



It gives you a tuple such as (2, 1) in your example. Pass that to df.iloc to get the value.






share|improve this answer























  • It works but it is not giving row and column names in output. Also will it work if there are some np.nan values in df?

    – rnso
    Nov 14 '18 at 6:21












  • @rnso: if you want to ignore NANs, simply use nanargmin instead of argmin. If you want the row and column names, you can use df.columns[x] and df.index[y] or df.iloc[[x], [y]] as in jezrael's answer.

    – John Zwinck
    Nov 14 '18 at 7:08


















1














Or min+min+dropna+T+dropna+T:



>>> df[df==df.min(axis=1).min()].dropna(how='all').T.dropna().T
C2
R3 0.0
>>>





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%2f53294115%2fget-row-and-column-with-minimum-value-in-entire-pandas-dataframe%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Use:



    a, b = df.stack().idxmin()
    print(df.loc[[a], [b]])
    C2
    R3 0


    Another @John Zwinck solution working with missing values - use numpy.nanargmin:



    df = pd.DataFrame(data=[[4,5,6],[2,np.nan,3],[7,0,5],[2,5,3]], 
    index = ['R1','R2','R3','R4'],
    columns=['C1','C2','C3'])

    print(df)
    C1 C2 C3
    R1 4 5.0 6
    R2 2 NaN 3
    R3 7 0.0 5
    R4 2 5.0 3

    #https://stackoverflow.com/a/3230123
    ri, ci = np.unravel_index(np.nanargmin(df.values), df.shape)
    print(df.iloc[[ri], [ci]])
    C2
    R3 0.0





    share|improve this answer

























    • Great. Forgot to add in question: there are some np.nan values in real df. Will this code work there as well?

      – rnso
      Nov 14 '18 at 6:17











    • @rnso - sure, pandas function working with nans nice.

      – jezrael
      Nov 14 '18 at 6:17











    • @rnso - changed solution for working with missing values.

      – jezrael
      Nov 14 '18 at 6:52















    2














    Use:



    a, b = df.stack().idxmin()
    print(df.loc[[a], [b]])
    C2
    R3 0


    Another @John Zwinck solution working with missing values - use numpy.nanargmin:



    df = pd.DataFrame(data=[[4,5,6],[2,np.nan,3],[7,0,5],[2,5,3]], 
    index = ['R1','R2','R3','R4'],
    columns=['C1','C2','C3'])

    print(df)
    C1 C2 C3
    R1 4 5.0 6
    R2 2 NaN 3
    R3 7 0.0 5
    R4 2 5.0 3

    #https://stackoverflow.com/a/3230123
    ri, ci = np.unravel_index(np.nanargmin(df.values), df.shape)
    print(df.iloc[[ri], [ci]])
    C2
    R3 0.0





    share|improve this answer

























    • Great. Forgot to add in question: there are some np.nan values in real df. Will this code work there as well?

      – rnso
      Nov 14 '18 at 6:17











    • @rnso - sure, pandas function working with nans nice.

      – jezrael
      Nov 14 '18 at 6:17











    • @rnso - changed solution for working with missing values.

      – jezrael
      Nov 14 '18 at 6:52













    2












    2








    2







    Use:



    a, b = df.stack().idxmin()
    print(df.loc[[a], [b]])
    C2
    R3 0


    Another @John Zwinck solution working with missing values - use numpy.nanargmin:



    df = pd.DataFrame(data=[[4,5,6],[2,np.nan,3],[7,0,5],[2,5,3]], 
    index = ['R1','R2','R3','R4'],
    columns=['C1','C2','C3'])

    print(df)
    C1 C2 C3
    R1 4 5.0 6
    R2 2 NaN 3
    R3 7 0.0 5
    R4 2 5.0 3

    #https://stackoverflow.com/a/3230123
    ri, ci = np.unravel_index(np.nanargmin(df.values), df.shape)
    print(df.iloc[[ri], [ci]])
    C2
    R3 0.0





    share|improve this answer















    Use:



    a, b = df.stack().idxmin()
    print(df.loc[[a], [b]])
    C2
    R3 0


    Another @John Zwinck solution working with missing values - use numpy.nanargmin:



    df = pd.DataFrame(data=[[4,5,6],[2,np.nan,3],[7,0,5],[2,5,3]], 
    index = ['R1','R2','R3','R4'],
    columns=['C1','C2','C3'])

    print(df)
    C1 C2 C3
    R1 4 5.0 6
    R2 2 NaN 3
    R3 7 0.0 5
    R4 2 5.0 3

    #https://stackoverflow.com/a/3230123
    ri, ci = np.unravel_index(np.nanargmin(df.values), df.shape)
    print(df.iloc[[ri], [ci]])
    C2
    R3 0.0






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 6:51

























    answered Nov 14 '18 at 6:15









    jezraeljezrael

    333k24276352




    333k24276352












    • Great. Forgot to add in question: there are some np.nan values in real df. Will this code work there as well?

      – rnso
      Nov 14 '18 at 6:17











    • @rnso - sure, pandas function working with nans nice.

      – jezrael
      Nov 14 '18 at 6:17











    • @rnso - changed solution for working with missing values.

      – jezrael
      Nov 14 '18 at 6:52

















    • Great. Forgot to add in question: there are some np.nan values in real df. Will this code work there as well?

      – rnso
      Nov 14 '18 at 6:17











    • @rnso - sure, pandas function working with nans nice.

      – jezrael
      Nov 14 '18 at 6:17











    • @rnso - changed solution for working with missing values.

      – jezrael
      Nov 14 '18 at 6:52
















    Great. Forgot to add in question: there are some np.nan values in real df. Will this code work there as well?

    – rnso
    Nov 14 '18 at 6:17





    Great. Forgot to add in question: there are some np.nan values in real df. Will this code work there as well?

    – rnso
    Nov 14 '18 at 6:17













    @rnso - sure, pandas function working with nans nice.

    – jezrael
    Nov 14 '18 at 6:17





    @rnso - sure, pandas function working with nans nice.

    – jezrael
    Nov 14 '18 at 6:17













    @rnso - changed solution for working with missing values.

    – jezrael
    Nov 14 '18 at 6:52





    @rnso - changed solution for working with missing values.

    – jezrael
    Nov 14 '18 at 6:52













    1














    I'd get the index this way:



    np.unravel_index(np.argmin(df.values), df.shape)


    This is much faster than df.stack().idxmin().



    It gives you a tuple such as (2, 1) in your example. Pass that to df.iloc to get the value.






    share|improve this answer























    • It works but it is not giving row and column names in output. Also will it work if there are some np.nan values in df?

      – rnso
      Nov 14 '18 at 6:21












    • @rnso: if you want to ignore NANs, simply use nanargmin instead of argmin. If you want the row and column names, you can use df.columns[x] and df.index[y] or df.iloc[[x], [y]] as in jezrael's answer.

      – John Zwinck
      Nov 14 '18 at 7:08















    1














    I'd get the index this way:



    np.unravel_index(np.argmin(df.values), df.shape)


    This is much faster than df.stack().idxmin().



    It gives you a tuple such as (2, 1) in your example. Pass that to df.iloc to get the value.






    share|improve this answer























    • It works but it is not giving row and column names in output. Also will it work if there are some np.nan values in df?

      – rnso
      Nov 14 '18 at 6:21












    • @rnso: if you want to ignore NANs, simply use nanargmin instead of argmin. If you want the row and column names, you can use df.columns[x] and df.index[y] or df.iloc[[x], [y]] as in jezrael's answer.

      – John Zwinck
      Nov 14 '18 at 7:08













    1












    1








    1







    I'd get the index this way:



    np.unravel_index(np.argmin(df.values), df.shape)


    This is much faster than df.stack().idxmin().



    It gives you a tuple such as (2, 1) in your example. Pass that to df.iloc to get the value.






    share|improve this answer













    I'd get the index this way:



    np.unravel_index(np.argmin(df.values), df.shape)


    This is much faster than df.stack().idxmin().



    It gives you a tuple such as (2, 1) in your example. Pass that to df.iloc to get the value.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 6:18









    John ZwinckJohn Zwinck

    152k16176291




    152k16176291












    • It works but it is not giving row and column names in output. Also will it work if there are some np.nan values in df?

      – rnso
      Nov 14 '18 at 6:21












    • @rnso: if you want to ignore NANs, simply use nanargmin instead of argmin. If you want the row and column names, you can use df.columns[x] and df.index[y] or df.iloc[[x], [y]] as in jezrael's answer.

      – John Zwinck
      Nov 14 '18 at 7:08

















    • It works but it is not giving row and column names in output. Also will it work if there are some np.nan values in df?

      – rnso
      Nov 14 '18 at 6:21












    • @rnso: if you want to ignore NANs, simply use nanargmin instead of argmin. If you want the row and column names, you can use df.columns[x] and df.index[y] or df.iloc[[x], [y]] as in jezrael's answer.

      – John Zwinck
      Nov 14 '18 at 7:08
















    It works but it is not giving row and column names in output. Also will it work if there are some np.nan values in df?

    – rnso
    Nov 14 '18 at 6:21






    It works but it is not giving row and column names in output. Also will it work if there are some np.nan values in df?

    – rnso
    Nov 14 '18 at 6:21














    @rnso: if you want to ignore NANs, simply use nanargmin instead of argmin. If you want the row and column names, you can use df.columns[x] and df.index[y] or df.iloc[[x], [y]] as in jezrael's answer.

    – John Zwinck
    Nov 14 '18 at 7:08





    @rnso: if you want to ignore NANs, simply use nanargmin instead of argmin. If you want the row and column names, you can use df.columns[x] and df.index[y] or df.iloc[[x], [y]] as in jezrael's answer.

    – John Zwinck
    Nov 14 '18 at 7:08











    1














    Or min+min+dropna+T+dropna+T:



    >>> df[df==df.min(axis=1).min()].dropna(how='all').T.dropna().T
    C2
    R3 0.0
    >>>





    share|improve this answer



























      1














      Or min+min+dropna+T+dropna+T:



      >>> df[df==df.min(axis=1).min()].dropna(how='all').T.dropna().T
      C2
      R3 0.0
      >>>





      share|improve this answer

























        1












        1








        1







        Or min+min+dropna+T+dropna+T:



        >>> df[df==df.min(axis=1).min()].dropna(how='all').T.dropna().T
        C2
        R3 0.0
        >>>





        share|improve this answer













        Or min+min+dropna+T+dropna+T:



        >>> df[df==df.min(axis=1).min()].dropna(how='all').T.dropna().T
        C2
        R3 0.0
        >>>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 6:23









        U9-ForwardU9-Forward

        15.2k41438




        15.2k41438



























            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%2f53294115%2fget-row-and-column-with-minimum-value-in-entire-pandas-dataframe%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto