Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing pandas dataframe with rows conditional on their not existing in another dataframe python

I have a pandas dataframe

df
store    day   items
 a        1     4
 a        1     3
 a        2     1
 a        3     5
 a        4     2 
 a        5     9
 b        1     1 
 b        2     3

I have another pandas dataframe temp that is the kronecker product of all unique store-day combinations, that is, it looks like:

    store  day  
0     a    1     
1     a    2      
2     a    3      
3     a    4      
4     a    5      
5     b    1      
6     b    2      
7     b    3    
8     b    4    
9     b    5    

I want to make a new DF that is the missing observations in df, that is, the store-day combinations not present in df but present in temp.

desired output


store    day
b         3      
b         4       
b         5      
like image 535
wolfsatthedoor Avatar asked Feb 22 '26 12:02

wolfsatthedoor


1 Answers

This is one way

gcols = ['store', 'date']
tmp[tmp.set_index(gcols).index.isin(df.set_index(gcols).index) == False]
like image 74
user1827356 Avatar answered Feb 24 '26 02:02

user1827356



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!