[Python] How to find values appear the most

Discussão em 'Python' iniciado por Stack, Agosto 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe that has the number 6 in each row. Which will be my main number I would like to use
    to find values that appear most often with the number 6 that has more than 2 of the same values. Here is my dataframe below,


    import pandas as pd

    df = pd.DataFrame([[1,2,4,5,6,8],
    [5,6,20,22,23,34],
    [6,12,13,34,45,46],
    [4,6,10,29,32,33],
    [1,6,13,23,33,35],
    [1,2,5,6,9,10],
    [1,2,3,5,6,8]],
    columns = ['Num1','Num2','Num3','Num4','Num5','Num6'])


    I have tried this code below which is close to what I'm looking for. However, I would like to return the number 6, values, and values count.

    df1 = df.stack().value_counts()
    df2 = df1[df1 >= 2]


    I would like my results to be like this below if possible or similar

    import pandas as pd

    result = pd.DataFrame([[6,1,4],
    [6,5,4],
    [6,2,3],
    [6,4,2],
    [6,8,2],
    [6,10,2],
    [6,13,2],
    [6,23,2],
    [6,33,2],
    [6,34,2]],
    columns = ['Num1','Values','Count'])

    Continue reading...

Compartilhe esta Página