How to look for a value in a Python DataFrame column?
<p>This is my first question on this forum, sorry if my English is not so good!</p>

<p>I want to add a row to a DataFrame only if a specific column doesn't already contain a specific value. Let say I write this :</p>

<pre><code>df = pd.DataFrame([['Mark', 9], ['Laura', 22]], columns=['Name', 'Age'])
new_friend = pd.DataFrame([['Alex', 23]], columns=['Name', 'Age'])
df = df.append(new_friend, ignore_index=True)
print(df)

    Name  Age
0   Mark    9
1  Laura   22
2   Alex   23
</code></pre>

<p>Now I want to add another friend, but I want to make sure I don't already have a friend with the same name. Here is what I'm actually doing:</p>

<pre><code>new_friend = pd.DataFrame([['Mark', 16]], columns=['Name', 'Age'])
df = df.append(new_friend, ignore_index=True)
print(df)

    Name  Age
0   Mark    9
1  Laura   22
2   Alex   55
3   Mark   16
</code></pre>

<p>then :</p>

<pre><code>df = df.drop_duplicates(subset='Name', keep='first')
df = df.reset_index(drop=True)
print(df)

    Name  Age
0   Mark    9
1  Laura   22
2   Alex   55
</code></pre>

<p>Is there another way of doing this something like : </p>

<p>if name in Column 'Name'is True:</p>

<p>don't add friend</p>

<p>else:</p>

<p>add friend</p>

<p>Thank you!</p>

<pre><code>if 'Mark' in list(df['Name']):
    print('Mark already in DF')
else:
    print('Mark not in DF')
</code></pre>

python pandas dataframe