1

I have a dataframe of values.

mydf.head():

    Amount Date        Description

0   39.95  2017-03-30  Paypal *A2zkidsltd     
1    2.39  2017-04-01  Mcdonalds              
2    8.03  2017-04-01  Wm Morrison
3   34.31  2017-04-01  Wm Morrison
4   10.56  2017-04-03  Asda Superstore

I wish to replace all "*" with nothing. I use the pandas replace function on the Description column.

mydf['Description'].replace('*','', inplace=True)

And look again at the result:

mydf.head()

0   39.95 2017-03-30  Paypal *A2zkidsltd     
1    2.39 2017-04-01  Mcdonalds              
2    8.03 2017-04-01  Wm Morrison
3   34.31 2017-04-01  Wm Morrison
4   10.56 2017-04-03  Asda Superstore

The first transaction still contains a "*".

This is seen for all transactions containing "*".

I have looked at the documentation;

https://pandas.pydata.org/pandas-docs/version/0.22.0/generated/pandas.DataFrame.replace.html

However I do not see where I am going wrong. What is the error in my code or my approach?

acolls_badger
  • 423
  • 1
  • 9
  • 29
  • 1
    I think you need `str` you can try: `df['Description'] = df['Description'].str.replace('*','')` – niraj Jun 19 '18 at 13:56
  • The reason it does not work as expected is because `pandas.DataFrame.replace` matches the entire string value to replace it. – neurite Jun 19 '18 at 14:01

2 Answers2

2

You can try:

df['Description'] = df['Description'].str.replace('*','')

There does not seem to be option for inplace so, need to assign to column again.

niraj
  • 17,498
  • 4
  • 33
  • 48
0

Try escaping it - * is a special character :)

mydf['Description'].replace('\*','', inplace=True)

Or as Sacul suggests:

mydf['Description'].replace('*','', regex=False, inplace=True)
Roelant
  • 4,508
  • 1
  • 32
  • 62