0

I have a dataset , in which one of the columns is Degree, from that column i want to looking for a particular keyword and for that keyword i want to add a short form in a list. My initial code works fine for me:

c = []

for words in df['Degree'].values:
    
    if "(m.tech)" in words.lower():
        c.append("M.Tech")
    elif "m.tech" in words.lower():
        c.append("M.Tech")
    elif "(m.sc)" in words.lower():
        c.append("M.Sc")
    elif "m.sc" in words.lower():
        c.append("M.Sc")
    elif "post" in words.lower():
        c.append("PG")
    elif "(b.tech)" in words.lower():
        c.append("B.Tech")
    elif "(b.e)" in words.lower():
        c.append("B.Tech")
    elif "b.tech" in words.lower():
        c.append("B.Tech")
    elif "b.e" in words.lower():
        c.append("B.Tech")
    else:
        c.append(np.nan)

But i want to make it small using "OR" . so i wrote:

d = []

for words in df['Degree'].values:
    if "(m.tech)" or "m.tech" in words.lower():
        d.append("M.tech")
    elif "(m.sc)" or "m.sc" in words.lower():
        d.append("M.Sc")
    elif "(b.tech)" or "b.tech" or "b.e" or "(b.e)" in words.lower():
        d.append("B.Tech")
    elif "post" or "pg" in words.lower():
        d.append("PG")
    else:
        d.append(np.nan)
        

In this piece of code, what i get is only "M.Tech" values , even if if has nan value.

Any help ?

Ayan Chowdhury
  • 229
  • 2
  • 8
  • 1
    By the way, `m.tech` is part of `(m.tech)` so just checking for the former will cover both cases... So your first two cases can be easily simplified. For the others, see the above links – Tomerikoo Nov 03 '20 at 07:58
  • Thank you for the help. I am grateful :) – Ayan Chowdhury Nov 03 '20 at 08:01

0 Answers0