Home > database >  adding a conditional column to a data frame
adding a conditional column to a data frame

Time:06-08

I have a data frame containing the headlines of news articles (Title) and links(Link)

I want to add a tekker column; basically i have a list of tekkers. List=["Saham Assurance","Attijariwafa bank"].

Everytime a tekker appears in a headline, the value of the row of this new column will be said tekker:

This is the expected result:

Title Link Tekker
Saham Assurance: Repli de 6% du chiffre d'affaires à fin septembre link1 Saham Assurance
Société anonyme : Tour de vis dans le contrôle des conventions réglementées link2 NaN
Attijariwafa bank : Baisse drastique du coût du risque link3 Attijariwafa bank

I tried this method :

df["Tekker"]=np.nan
List=["Saham Assurance","Attijariwafa bank"]
for element in df["title"]:
    i=0
    for tekker in List:
        if tekker  in element:
            df.Tekker[i]=tekker
    i=i 1 

But it does not work as expected. I am new to python, so I am not sure what I am doing wrong.

Any help will be appreciated.

CodePudding user response:

i think this one should work. I assume you expect only one tekker per title:

df['Tekker'] = df['Title'].apply(lambda x:t for t in List if t in x)
  • Related