-2

I am trying to create a new column that checks if there's a string in my values using the following method:

 user['validacao_LP']=user['MEDIA_SOURCE'].str.contains('LP')

But I keep getting the following message:

<ipython-input-27-841ac3ae43d8>: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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  user['validacao_LP']=user['MEDIA_SOURCE'].str.contains('LP')

I have already tried to use the lines bellow aftwer reading the documentation but I keep getting the same message.

user.loc[:,'validacao_LP']=user.loc[:,'MEDIA_SOURCE'].str.contains('LP')
user['validacao_LP']=user.loc[:,'MEDIA_SOURCE'].str.contains('LP')
user.loc[:,'validacao_LP']=user['MEDIA_SOURCE'].str.contains('LP')
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • 2
    did you check out [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/q/20625582/10197418)? – FObersteiner Jan 12 '22 at 12:56
  • 3
    Looks like the dataframe `user` is itself already a copy of another dataframe, hence pandas says that `user['validacao_LP']` is a slice of a copy, which you can't assign to. **Show us the code that defines `user`.** – smci Jan 12 '22 at 12:57
  • That was it, I had a line above defining user as a copy a previous user df. Corrected it and it worked. Thank you so much! – Juliana Pereira de Souza Jan 12 '22 at 13:14

1 Answers1

0

Looks like the dataframe user is itself already a copy of another dataframe, hence pandas warns that user['validacao_LP'] is a slice of a copy, which you can't assign to.

Check the code that defines user.

smci
  • 32,567
  • 20
  • 113
  • 146