0

I have a pandas dataframe, and I want to create a new column with values 'in list' or 'not in list', based on whether an entry in the first column is in a list. To illustrate I have a toy example below. I have a solution which works, however it seems very cumbersome and not very pythonic. I do also get a SettingWithCopyWarning. Is there a better or more recommended way to achieve this in python?

#creating a toy dataframe with one column
df = pd.DataFrame({'col_1': [1,2,3,4,6]})

#the list we want to check if any value in col_1 is in 
list_ = [2,3,3,3]

#creating a new empty column
df['col_2'] = None
    col_1   col_2
0   1   None
1   2   None
2   3   None
3   4   None
4   6   None

My solution is to loop through the first column and populate the second

for index, i in enumerate(df['col_1']):
    if i in list_:
        df['col_2'].iloc[index] = 'in list'
    else:
        df['col_2'].iloc[index] = 'not in list'
    col_1   col_2
0   1   not in list
1   2   in list
2   3   in list
3   4   not in list
4   6   not in list

Which produces the correct result, but I would like to learn a more pythonic way of achieving this.

0

1 Answer 1

1

Use Series.isin with Series.map:

In [1197]: df['col_2'] = df.col_1.isin(list_).map({False: 'not in list', True: 'in list'})

In [1198]: df
Out[1198]: 
   col_1        col_2
0      1  not in list
1      2      in list
2      3      in list
3      4  not in list
4      6  not in list
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.