1

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

mozway
  • 194,879
  • 13
  • 39
  • 75
Merve
  • 43
  • 5

2 Answers2

1

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
mozway
  • 194,879
  • 13
  • 39
  • 75
0
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)
  • You should almost never have to loop with pandas, this is an anti-pattern! Also using `in my_list` repeatedly is quadratic complexity. – mozway Sep 23 '22 at 13:35