How to assign new column based on the list of string values in pandas

Question:

I have a dataframe that one of the column contains string values, and I want to assign new column if this column values are in the list I specified.

my_list = [‘AA’, ‘TR’, ‘NZ’]

For example:
My dataframe : df

country
AA
TR
SG

The dataframe I want to have:

country flag
AA 1
TR 1
SG 0

I tried this one but I gave an Value Error.

df.assign(flag = lambda df: '1' if df['country'].isin(my_list) else '0')

What should I do? Thank you

Asked By: Merve

||

Answers:

You can directly convert your boolean to 0/1 with astype(int):

df.assign(flag= df['country'].isin(my_list).astype(int))

Or for a string:

df.assign(flag= df['country'].isin(my_list).astype(int).astype(str))

Alternatively, use numpy.where:

import numpy as np
df.assign(flag=np.where(df['country'].isin(my_list), '1', '0'))

output:

  country flag
0      AA    1
1      TR    1
2      SG    0
Answered By: mozway
import pandas as pd

my_list = ['AA', 'TR', 'NZ']
df = pd.DataFrame(columns=["Country", "Flag"])

df.loc[0] = ["AA", 0]
df.loc[1] = ["TR", 0]
df.loc[2] = ["SG", 0]

print(df)

for i in range(len(df)):
    if df["Country"].iloc[i] in my_list:
        df["Flag"].iloc[i] = 1

print(df)
Answered By: Stef Renneboog