-1

I have a CSV and would like to replace specific values in a Pandas column. It's working fine vor all values except for the likes of "5-6" and "2-3".

df["v_82"].replace({"5-6": "5", "2007": "13"}, inplace=True)

I copied these problematic values as text using "value_counts" and then pasted them into the replace function, so it can't be a typo.

df.v_82.value_counts ()

When I open up the original CSV in Excel, these problematic cell values are interpreted as dates, not as pure text volumes.

Thanks for helping out here.

journo
  • 99
  • 5
  • 2
    post the sample dataframe – deadshot Jul 08 '20 at 10:29
  • 1
    you most likely have white space in your string i.e i'ts `' 5 -6 '` not `'5-6'` just tested your code locally and was not able to reproduce. also don't use `inplace=True` re-assign your variable – Umar.H Jul 08 '20 at 10:30
  • see [Don't use inplace=True!](https://stackoverflow.com/a/59242208/9375102) by cs95 – Umar.H Jul 08 '20 at 10:31
  • Thx. What's the usual way to post sample dataframes here if its a CSV? – journo Jul 08 '20 at 10:42
  • 1
    @Datanovice You were correct. There were trailing white spaces in the string which I overlooked because I just searched the original CSV for "5-6". Thx for making me look one more time. – journo Jul 08 '20 at 10:48

1 Answers1

0

The answer is as simple as this:

you most likely have white space in your string i.e i'ts ' 5 -6 ' not '5-6'

I had copied/pasted only part of the string, leaving out a trailing white space.

journo
  • 99
  • 5
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/26620665) – Celius Stingher Jul 08 '20 at 13:08
  • @CeliusStingher To me, this provided the answer to my question. It was [posted as comment](https://stackoverflow.com/questions/62792706/pandas-replace-doesnt-work-with-cell-value-5-6/62793145?noredirect=1#comment111042368_62792706) to my question, so I could not mark it as an answer. How could I have handled it more properly in this case? – journo Jul 09 '20 at 10:38
  • But this seems not to be your answer, instead Datanovice's comment. Furthermore, the answer should contain the correct code to solve the issue and preferably the example output showing the working solution. – Celius Stingher Jul 09 '20 at 12:28