1

I everyone, I've gotten the dreaded "A value is trying to be set on a copy of a slice from a DataFrame." error.

I saw a possible solution here How to deal with SettingWithCopyWarning in Pandas but it doesn't work with my example, since I'm trying to also use strip().

In a df with several columns, the type of "column_X" is object. I plan on later changing the strings to floats or integers. Currently, some values have a period followed by one or two zeros. So I want to strip everything starting from the period.

column_x column_y
"1.00" 35
"15.0" 456
"2.00" 32
"35.00" 789

Here's my code:

df['column_x'] = df['column_x'].map(lambda x: x.strip('.'))

Desired output:

column_x column_y
"1" 35
"15" 456
"2" 32
"35" 789

Actual output:

"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"

I'm not sure how to use .loc because most examples seem to have conditions while my code uses .strip() while mapping.

Any help would be greatly appreciated!

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
Celia
  • 45
  • 4
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. [Include your minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. – Prune Mar 28 '21 at 15:24
  • Hi Prune, I will try to follow MRE. Do that right now. Thank you for your comment. – Celia Mar 28 '21 at 15:26

0 Answers0