3

Imagine I have a list that represents the indexes of a dataframe, like this one:

indexlist = [0,1,4]

And the following dataframe:

    Name    Country
0   John    BR
1   Peter   BR
2   Paul    BR
3   James   CZ
4   Jonatan CZ
5   Maria   DK

I need to create a column on this dataframe named "Is it valid?" that would add "Yes" if the index row is in the list. Otherwise would add "No", resulting in this dataframe:

    Name    Country  Is it valid?
0   John    BR       Yes
1   Peter   BR       Yes
2   Paul    BR       No
3   James   CZ       No
4   Jonatan CZ       Yes
5   Maria   DK       No

Is there any way I could do it?

Thanks!

1
  • Instead of a fake boolean (Yes/No), you should consider using an actual boolean (True/False). Then this is simplified to df['Is it valid?'] = df.index.isin(indexlist) Commented Sep 8, 2022 at 20:51

2 Answers 2

5

You can use isin for index, that you'd normally use with Series, it essentially creates an array of truth value, which you can pass to np.where with true and false values, assign the result as a column.

df['Is it valid?'] = np.where(df.index.isin(indexlist), 'Yes', 'No')

OUTPUT:

      Name Country Is it valid?
0     John      BR          Yes
1    Peter      BR          Yes
2     Paul      BR           No
3    James      CZ           No
4  Jonatan      CZ          Yes
5    Maria      DK           No
Sign up to request clarification or add additional context in comments.

Comments

2

you can use iloc

df['Is it valid?']='No'
df['Is it valid?'].iloc[indexlist] = 'Yes'

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.