0

When performing the following piece of code, I get this error:

input

df['col1'] = df['col1'].apply(lambda x: float(x.replace('/', '')))

output

/..somepath.../lib/python2.7/site-packages/pandas/core/indexing.py:601: 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
self.obj[item_labels[indexer[info_axis]]] = value

There are a few similar questions on the subject, such as this one, however none of them include more complex issues such as column string manipulation through lambda. Also, in the answers there, once .loc is used, the issue seems to be fixed, whereas here it persists.

I went through the documentation and it seems that I need to use .loc, so I did (see code below), however my issues still persists.

df.loc[:,('col1')] = df.loc[:,('col1')].apply(lambda x: float(x.replace('/', '')))

Could you tell me if this is a different problem that I am having and how it can be fixed, so that I know better going forward?

EDIT: previous code

Below is the df initialization and a few extra lines of code before the like with the problem

d = df[df['col0'].str.contains('text')]
if not df.empty:

I generally tend to not use .loc for all DataFrame operations, as I find it less readable. Could this be the issue?

Newskooler
  • 3,973
  • 7
  • 46
  • 84

0 Answers0