[Python] Python pandas Dataframe comparison

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    People of stack overflow, help!

    I have a leetcode style problem for you guys.

    Imagine a scenario where you have 2 2D arrays, more specifically 2 Dataframes with pandas.

    I need to compare these 2 Dataframes and highlight all the differences, however there is a catch. Rows can be missing from these data frames which makes this inherently a lot more difficult, as well as missing cells too. I'll provide an example.

    import pandas as pd

    x = [[0, 1, 2, 3],[4, 5, 6, 7],[8, 9, 10, 11],[12, 13, 14, 15]]
    y = [[nan, 1, 2, 3],[4, 5, 6, nan],[12, 13, 14, 15]]

    df1 = pd.DataFrame(x)
    df2 = pd.DataFrame(y)


    How can I identify all of the missing cells AND the missing rows?

    Bonus points if you can create code to highlight the differences and export them to an excel sheet ;)

    Continue reading...

Compartilhe esta Página