0

enter image description here

Column name = "Insurance Type"

3 types of names in the columns "wa, wa_bep_ca" and "wa_ca".

Trying to replace these names with proper names. Any idea on why they don't get all replaced in the column?? Thanks for your support.

Jai
  • 819
  • 7
  • 17
  • Since your are applying these methods on a Series, use the Series method: `str.replace` instead of `replace`, not 100% sure, but that should also solve the problem. – Erfan Mar 18 '20 at 15:58
  • Are there leading/trailing whitespace characters? Have you tried `.str.strip()` before the replacement? – G. Anderson Mar 18 '20 at 15:59
  • 1
    If you posted the code and data as text, people could run your example code for themselves and would probably be able to post a solution very quickly. By posting code and data as a picture, that makes things more difficult for everyone. [This](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) might help. – David Buck Mar 18 '20 at 15:59

1 Answers1

0

Use replace on the column of interest, with a dictionary specifying:

  • the string to find as keys,
  • the string to replace as values.

The code to do it is:

df['Insurance type'].replace({
    'wa_bep_ca': 'Liability + lim casco',
    'wa_ca': 'Liability + full casco',
    'wa': 'Liability only' }, inplace=True)

For the test I used the following DataFrame:

   offer_number Insurance type
0          1001             wa
1          1002      wa_bep_ca
2          1003          wa_ca
3          1004          wa_xx

The result is:

   offer_number          Insurance type
0          1001          Liability only
1          1002   Liability + lim casco
2          1003  Liability + full casco
3          1004                   wa_xx

(the last row does not match any of keys used, so it has not been replaced).

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41