Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering the dataframe based on the column value of another dataframe

I have 2 dataframes

df1

Company           SKU   Sales
Walmart           A     100
Total             A     200
Walmart           B     200
Total             B     300
Walmart           C     400
Walmart           D     500

df2

 Company             SKU   Sales
 Walmart             A     400
 Total               B     300
 Walmart             C     900
 Walmart             F     400
 Total               G     500

I want a resulting dataframe (df2) which only has the records of matching SKUs in df1 and df2

df2

Company       SKU   Sales 
Walmart       A     400
Total         B     300
Walmart       C     900

I want only the unique (Company + SKU) values of df1 in df2

Is there any good solution to achieve this?

like image 775
Ahamed Moosa Avatar asked Jun 02 '18 09:06

Ahamed Moosa


Video Answer


3 Answers

Update

You could use a simple mask:

m = df2.SKU.isin(df1.SKU)
df2 = df2[m]

You are looking for an inner join. Try this:

df3 = df1.merge(df2, on=['SKU','Sales'], how='inner')

#  SKU  Sales
#0   A    100
#1   B    200
#2   C    300

Or this:

df3 = df1.merge(df2, on='SKU', how='inner')

#  SKU  Sales_x  Sales_y
#0   A      100      100
#1   B      200      200
#2   C      300      300
like image 78
Anton vBR Avatar answered Oct 27 '22 19:10

Anton vBR


One way is to align indices and then use a mask.

# align indices
df1 = df1.set_index(['Company',  'SKU'])
df2 = df2.set_index(['Company',  'SKU'])

# calculate & apply mask
df2 = df2[df2.index.isin(df1.index)].reset_index()

Resetting index is not required, but needed to elevate Company and SKU to columns.

like image 42
jpp Avatar answered Oct 27 '22 21:10

jpp


Solution 1 :

# First identify the common SKU's    
temp = list(set(list(df1.SKU)).intersection(set(list(df2.SKU))))

# Filter df2 using the list of common SKU's
df3 = df2[df2.SKU.isin(temp)]
print(df3)

   SKU  Sales
0   A   400
1   B   300
2   C   900

Solution 2 : One Line solution

df3 = df2[df2.SKU.isin(list(df1.SKU))]

EDIT 1 : Solution for the updated question (Not the optimal way of doing it, but answers your question)

# reading data for df1
df1= pd.read_clipboard(sep='\\s+')
df1
    Company SKU Sales
0   Walmart A   100
1   Total   A   200
2   Walmart B   200
3   Total   B   300
4   Walmart C   400
5   Walmart D   500

# reading data for df2
df2= pd.read_clipboard(sep='\\s+')
df2
Company SKU Sales
0   Walmart A   400
1   Total   B   300
2   Walmart C   900
3   Walmart F   400
4   Total   G   500

# Using intersect and zip to create a list of tuples matching in the data frames
temp = list(set(list(zip(df1.Company,df1.SKU))).intersection(set(list(zip(df2.Company,df2.SKU)))))
temp
[('Walmart', 'A'), ('Walmart', 'C'), ('Total', 'B')]

# Creating a helper variable in df2 to lookup in the temp list
df2["temp"] = list(zip(df2.Company,df2.SKU))
df2= df2[df2["temp"].isin(temp)]
del(df2["temp"])
df2
    Company SKU Sales
0   Walmart A   400
1   Total   B   300
2   Walmart C   900

Suggestions are welcome to improve this code

like image 3
Ram Avatar answered Oct 27 '22 19:10

Ram