Not able to replace the string containing $ in pandas column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have a dataframe



df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])


I want to replace $sell from column b.



So I tried replace() method like below



df['b'] = df['b'].str.replace("$sell","")


but it's doesn't replace the given string and it gives me same dataframe as original.



It's working when I use it with apply



df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))


So I want to know why it is not working in previous case?



Note: I tried replacing only $ and shockingly it works.










share|improve this question



























    up vote
    6
    down vote

    favorite












    I have a dataframe



    df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])


    I want to replace $sell from column b.



    So I tried replace() method like below



    df['b'] = df['b'].str.replace("$sell","")


    but it's doesn't replace the given string and it gives me same dataframe as original.



    It's working when I use it with apply



    df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))


    So I want to know why it is not working in previous case?



    Note: I tried replacing only $ and shockingly it works.










    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I have a dataframe



      df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])


      I want to replace $sell from column b.



      So I tried replace() method like below



      df['b'] = df['b'].str.replace("$sell","")


      but it's doesn't replace the given string and it gives me same dataframe as original.



      It's working when I use it with apply



      df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))


      So I want to know why it is not working in previous case?



      Note: I tried replacing only $ and shockingly it works.










      share|improve this question















      I have a dataframe



      df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])


      I want to replace $sell from column b.



      So I tried replace() method like below



      df['b'] = df['b'].str.replace("$sell","")


      but it's doesn't replace the given string and it gives me same dataframe as original.



      It's working when I use it with apply



      df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))


      So I want to know why it is not working in previous case?



      Note: I tried replacing only $ and shockingly it works.







      python string pandas series






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      jpp

      67k173984




      67k173984










      asked 2 hours ago









      Akshay Nevrekar

      2,29251331




      2,29251331






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          It is regex metacharacter (end of string), escape it or add parameter regex=False:



          df['b'] = df['b'].str.replace("$sell","")
          print (df)
          a b
          0 1 NaN
          1 2 12
          2 3 1



          df['b'] = df['b'].str.replace("$sell","", regex=False)


          If want also value 5, what is numeric, use Series.replace with regex=True for replace substrings - numeric values are not touched:



          df['b'] = df['b'].replace("$sell","", regex=True)

          print (df['b'].apply(type))
          0 <class 'int'>
          1 <class 'str'>
          2 <class 'str'>
          Name: b, dtype: object


          Or cast to strings all data of column:



          df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)

          print (df['b'].apply(type))
          0 <class 'str'>
          1 <class 'str'>
          2 <class 'str'>
          Name: b, dtype: object


          And for better performance if no missing values is possible use list comprehension:



          df['b'] = [str(x).replace("$sell","") for x in df['b']]

          print (df)
          a b
          0 1 5
          1 2 12
          2 3 1





          share|improve this answer





























            up vote
            4
            down vote













            df['b'] = df['b'].str.replace("$sell","", regex=False)





            share|improve this answer



























              up vote
              4
              down vote













              $ is a regex special character. By default, pd.Series.str.replace uses regex=True.



              Instead, specify regex=False:



              df['b'] = df['b'].str.replace('$sell', '', regex=False)





              share|improve this answer



























                up vote
                3
                down vote













                str.replace assumes a regex is being used. so you need to use escape i.e.



                df['b'] = df['b'].str.replace("$sell","")





                share|improve this answer




















                  Your Answer





                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52554917%2fnot-able-to-replace-the-string-containing-in-pandas-column%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  5
                  down vote



                  accepted










                  It is regex metacharacter (end of string), escape it or add parameter regex=False:



                  df['b'] = df['b'].str.replace("$sell","")
                  print (df)
                  a b
                  0 1 NaN
                  1 2 12
                  2 3 1



                  df['b'] = df['b'].str.replace("$sell","", regex=False)


                  If want also value 5, what is numeric, use Series.replace with regex=True for replace substrings - numeric values are not touched:



                  df['b'] = df['b'].replace("$sell","", regex=True)

                  print (df['b'].apply(type))
                  0 <class 'int'>
                  1 <class 'str'>
                  2 <class 'str'>
                  Name: b, dtype: object


                  Or cast to strings all data of column:



                  df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)

                  print (df['b'].apply(type))
                  0 <class 'str'>
                  1 <class 'str'>
                  2 <class 'str'>
                  Name: b, dtype: object


                  And for better performance if no missing values is possible use list comprehension:



                  df['b'] = [str(x).replace("$sell","") for x in df['b']]

                  print (df)
                  a b
                  0 1 5
                  1 2 12
                  2 3 1





                  share|improve this answer


























                    up vote
                    5
                    down vote



                    accepted










                    It is regex metacharacter (end of string), escape it or add parameter regex=False:



                    df['b'] = df['b'].str.replace("$sell","")
                    print (df)
                    a b
                    0 1 NaN
                    1 2 12
                    2 3 1



                    df['b'] = df['b'].str.replace("$sell","", regex=False)


                    If want also value 5, what is numeric, use Series.replace with regex=True for replace substrings - numeric values are not touched:



                    df['b'] = df['b'].replace("$sell","", regex=True)

                    print (df['b'].apply(type))
                    0 <class 'int'>
                    1 <class 'str'>
                    2 <class 'str'>
                    Name: b, dtype: object


                    Or cast to strings all data of column:



                    df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)

                    print (df['b'].apply(type))
                    0 <class 'str'>
                    1 <class 'str'>
                    2 <class 'str'>
                    Name: b, dtype: object


                    And for better performance if no missing values is possible use list comprehension:



                    df['b'] = [str(x).replace("$sell","") for x in df['b']]

                    print (df)
                    a b
                    0 1 5
                    1 2 12
                    2 3 1





                    share|improve this answer
























                      up vote
                      5
                      down vote



                      accepted







                      up vote
                      5
                      down vote



                      accepted






                      It is regex metacharacter (end of string), escape it or add parameter regex=False:



                      df['b'] = df['b'].str.replace("$sell","")
                      print (df)
                      a b
                      0 1 NaN
                      1 2 12
                      2 3 1



                      df['b'] = df['b'].str.replace("$sell","", regex=False)


                      If want also value 5, what is numeric, use Series.replace with regex=True for replace substrings - numeric values are not touched:



                      df['b'] = df['b'].replace("$sell","", regex=True)

                      print (df['b'].apply(type))
                      0 <class 'int'>
                      1 <class 'str'>
                      2 <class 'str'>
                      Name: b, dtype: object


                      Or cast to strings all data of column:



                      df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)

                      print (df['b'].apply(type))
                      0 <class 'str'>
                      1 <class 'str'>
                      2 <class 'str'>
                      Name: b, dtype: object


                      And for better performance if no missing values is possible use list comprehension:



                      df['b'] = [str(x).replace("$sell","") for x in df['b']]

                      print (df)
                      a b
                      0 1 5
                      1 2 12
                      2 3 1





                      share|improve this answer














                      It is regex metacharacter (end of string), escape it or add parameter regex=False:



                      df['b'] = df['b'].str.replace("$sell","")
                      print (df)
                      a b
                      0 1 NaN
                      1 2 12
                      2 3 1



                      df['b'] = df['b'].str.replace("$sell","", regex=False)


                      If want also value 5, what is numeric, use Series.replace with regex=True for replace substrings - numeric values are not touched:



                      df['b'] = df['b'].replace("$sell","", regex=True)

                      print (df['b'].apply(type))
                      0 <class 'int'>
                      1 <class 'str'>
                      2 <class 'str'>
                      Name: b, dtype: object


                      Or cast to strings all data of column:



                      df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)

                      print (df['b'].apply(type))
                      0 <class 'str'>
                      1 <class 'str'>
                      2 <class 'str'>
                      Name: b, dtype: object


                      And for better performance if no missing values is possible use list comprehension:



                      df['b'] = [str(x).replace("$sell","") for x in df['b']]

                      print (df)
                      a b
                      0 1 5
                      1 2 12
                      2 3 1






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 1 hour ago

























                      answered 1 hour ago









                      jezrael

                      290k19205283




                      290k19205283






















                          up vote
                          4
                          down vote













                          df['b'] = df['b'].str.replace("$sell","", regex=False)





                          share|improve this answer
























                            up vote
                            4
                            down vote













                            df['b'] = df['b'].str.replace("$sell","", regex=False)





                            share|improve this answer






















                              up vote
                              4
                              down vote










                              up vote
                              4
                              down vote









                              df['b'] = df['b'].str.replace("$sell","", regex=False)





                              share|improve this answer












                              df['b'] = df['b'].str.replace("$sell","", regex=False)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              Charles R

                              437211




                              437211




















                                  up vote
                                  4
                                  down vote













                                  $ is a regex special character. By default, pd.Series.str.replace uses regex=True.



                                  Instead, specify regex=False:



                                  df['b'] = df['b'].str.replace('$sell', '', regex=False)





                                  share|improve this answer
























                                    up vote
                                    4
                                    down vote













                                    $ is a regex special character. By default, pd.Series.str.replace uses regex=True.



                                    Instead, specify regex=False:



                                    df['b'] = df['b'].str.replace('$sell', '', regex=False)





                                    share|improve this answer






















                                      up vote
                                      4
                                      down vote










                                      up vote
                                      4
                                      down vote









                                      $ is a regex special character. By default, pd.Series.str.replace uses regex=True.



                                      Instead, specify regex=False:



                                      df['b'] = df['b'].str.replace('$sell', '', regex=False)





                                      share|improve this answer












                                      $ is a regex special character. By default, pd.Series.str.replace uses regex=True.



                                      Instead, specify regex=False:



                                      df['b'] = df['b'].str.replace('$sell', '', regex=False)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 1 hour ago









                                      jpp

                                      67k173984




                                      67k173984




















                                          up vote
                                          3
                                          down vote













                                          str.replace assumes a regex is being used. so you need to use escape i.e.



                                          df['b'] = df['b'].str.replace("$sell","")





                                          share|improve this answer
























                                            up vote
                                            3
                                            down vote













                                            str.replace assumes a regex is being used. so you need to use escape i.e.



                                            df['b'] = df['b'].str.replace("$sell","")





                                            share|improve this answer






















                                              up vote
                                              3
                                              down vote










                                              up vote
                                              3
                                              down vote









                                              str.replace assumes a regex is being used. so you need to use escape i.e.



                                              df['b'] = df['b'].str.replace("$sell","")





                                              share|improve this answer












                                              str.replace assumes a regex is being used. so you need to use escape i.e.



                                              df['b'] = df['b'].str.replace("$sell","")






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 1 hour ago









                                              sgDysregulation

                                              1,95611125




                                              1,95611125



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52554917%2fnot-able-to-replace-the-string-containing-in-pandas-column%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  What does second last employer means? [closed]

                                                  Installing NextGIS Connect into QGIS 3?

                                                  One-line joke