Ashim Pokharel
Ashim Pokharel

Reputation: 31

Python - Return third column value if two condition is matched pandas

I have Pandas DF like this:

Data

Now I need to get UTB1050_1PCS if the condition match Brother and _1PCS and UTB1050_2PCS if my condition if Brother and _2PCS.

Many thanks in advance!!!

Upvotes: 1

Views: 80

Answers (1)

A.J. Nock
A.J. Nock

Reputation: 132

Here is the data frame

import pandas as pd

list1=['_1PCS','_2PCS']
list2=['UTB1050_1PCS','UTB1050_2PCS']
list3=['Brother','Brother']

df=pd.DataFrame({'Variation':list1,
                 'Internal Code-Variation':list2,
                 'Brand':list3})

print(df)

Original Data

  Variation Internal Code-Variation    Brand
0     _1PCS            UTB1050_1PCS  Brother
1     _2PCS            UTB1050_2PCS  Brother

Filtered Data using Pandas .loc

import pandas as pd

list1=['_1PCS','_2PCS']
list2=['UTB1050_1PCS','UTB1050_2PCS']
list3=['Brother','Brother']

df=pd.DataFrame({'Variation':list1,
                 'Internal Code-Variation':list2,
                 'Brand':list3})

filtered = df.loc[(df['Brand']=='Brother') & (df['Variation']=='_2PCS')]

Output

 Variation Internal Code-Variation    Brand
1     _2PCS            UTB1050_2PCS  Brother

There are many ways to approach this problem, I also recommend looking into Indexing and Selecting

Upvotes: 1

Related Questions