Comparing two pandas dataframes for differences

后端 未结 8 1236
感情败类
感情败类 2020-11-30 02:53

I\'ve got a script updating 5-10 columns worth of data , but sometimes the start csv will be identical to the end csv so instead of writing an identical csvfile I want it to

8条回答
  •  情深已故
    2020-11-30 03:27

    This compares the values of two dataframes note the number of row/columns needs to be the same between tables

    comparison_array = table.values == expected_table.values
    print (comparison_array)
    
    >>>[[True, True, True]
        [True, False, True]]
    
    if False in comparison_array:
        print ("Not the same")
    
    #Return the position of the False values
    np.where(comparison_array==False)
    
    >>>(array([1]), array([1]))
    

    You could then use this index information to return the value that does not match between tables. Since it's zero indexed, it's referring to the 2nd array in the 2nd position which is correct.

提交回复
热议问题