Skip to content
Advertisement

Comparing two Dataframes with diff length to find difference in specific column

i have 2 dataframes have same columns with different len.

JavaScript

the reuslt i want to get:

JavaScript

and each df have length like this.

JavaScript

each dataframe has column named ‘name, id, type, len

i need to check those columns(name,type,len) in each df to compare ‘id‘ column whether it has same value or not.

so i tried like this.

JavaScript

I have above code for 2 different length DataFrame. first i tried with ‘iterrows()’ for comparing those two df, but it took too much time.(very slow)

i looked up for other ways to make it work better performance.

possible ways i found

  • option1 transform df to dict with to_dict() / compare those in nested for-loop

  • option2 transform df.series to ndarray / compare those in nested for-loop

is there any other better option? or any option to not using nested for-loop?

thx.

Advertisement

Answer

you can try merge, and if you are looking for records where ids do mismatch then the following is one way of achieving it:

JavaScript

I have used how=”inner” join, but based on need can use any of the following joins: {‘left’, ‘right’, ‘outer’, ‘inner’, ‘cross’}, default ‘inner’

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement