Pandas Index | difference method
Start your free 7-days trial now!
Pandas Index.difference(~) method returns a new Index containing elements in the index that are not present in the other provided index.
Parameters
1. other | Index or array-like
The other index to compare elements against.
2. sort | False or None | optional
Determines whether to sort the new Index returned. Defaults to None.
| Explanation | |
|---|---|
| 
 | Try to sort the new  | 
| 
 | Do not sort the new  | 
Return Value
A new Index containing elements in the index not contained in other.
Examples
Basic usage
To return a new index containing the differences between index a and b:
        
        
            
                
                
                    import pandas as pda = pd.Index(["d","b","c", "a"])b = pd.Index(["b","c"])a.difference(b)
                
            
            Index(['a', 'd'], dtype='object')
        
    Notice how the new index is sorted as by default sort=None.
sort
To return the differences between index a and b while not sorting the resulting index returned:
        
        
            
                
                
                    import pandas as pda = pd.Index(["d","b","c", "a"])b = pd.Index(["b","c"])a.difference(b, sort=False)
                
            
            Index(['d', 'a'], dtype='object')
        
    Notice how the new index is not sorted and 'd' and 'a' are returned in their original order within Index a.
