Leonardo Henriques
Leonardo Henriques

Reputation: 526

Why can't I change the values ​in one pandas column only?

I have a column in my data frame where I have emails and not emails.

with this slice I can only get the fields that are without email:

df[~df['email'].str.contains('@', case=False)]['email']

But when I try to replace it with a value of my preference:

df[~df['email'].str.contains('@', case=False)]['email'] = 'No'

The column does not receive the change.

I don't get any error, just the following warning:

/home/rockstar/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  """Entry point for launching an IPython kernel.

follows an image of my complete dataframe:

python

Also, df[~df['email'].str.contains('@',case=False)] = 'No' works perfectly but I end up losing data from the rest of the line

Upvotes: 0

Views: 413

Answers (1)

Gilseung Ahn
Gilseung Ahn

Reputation: 2614

Refer the following example code.

import pandas as pd
df = pd.DataFrame({"E-mail":["abc@de", "abcde"]})
df['E-mail'].loc[~df['E-mail'].str.contains('@', case = False)] = 'No'

Upvotes: 1

Related Questions