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!
CodePudding user response:
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
CodePudding user response:
Maybe something like
df[indexList]['Is it valid?'] = True
I suggest using a boolean value rather than strings.
CodePudding user response:
you can use iloc
df['Is it valid?']='No'
df['Is it valid?'].iloc[indexlist] = 'Yes'