0

Suppose I have a dataframe which looks like this:

                _id                       name                  features
0               574                      Test1          Reference:5dft55
1                57                      Test2           Reference:5sh85
2                52                      Test3                       675

Is it possible to dynamically search each series within the dataframe and check whether a series contains the word Reference: and if it does it should remove that so that the output can look like the dataframe below:

Expected Ouput:

                _id                       name                  features
0               574                      Test1                    5dft55
1                57                      Test2                     5sh85
2                52                      Test3                       675
jcoke
  • 1,555
  • 1
  • 13
  • 27
  • Use `df['url'] = df['url'].replace({'Reference:': ''}, regex=True)` – jezrael Feb 17 '21 at 13:49
  • 1
    My question is a similar topic but completely different, I'm going for a dynamic approach, the other post is static which means they can explicitly specify the column name. I cannot specify the column name so it needs to manually check the dataframe whether that phrase is contained anywhere – jcoke Feb 17 '21 at 13:52
  • 1
    `df['url']` - you are explicitly specifying a column name, something that is dynamic means you cannot just type a column name, the code has to manually find the column which has the word `reference:` in it and then get the substring of it – jcoke Feb 17 '21 at 13:56
  • So if use `df = df.replace({'Reference:': ''}, regex=True)` working well ? – jezrael Feb 17 '21 at 13:57
  • A being the DF name ..you may try .. A = A.apply(lambda y:y if y.dtype=='int' or y.dtype=='float' else y.apply(lambda x:x if 'Reference' not in x else x.split("Reference:")[-1])) – Amit Feb 17 '21 at 14:04
  • that doesn't seem to be working for me – jcoke Feb 17 '21 at 14:21
  • @Amit df.replace doesn't do partial matches so if it's not how the string is actually specified in the replace it wont do anything, I need to check if the column contains that word, and if it does then replace it – jcoke Feb 17 '21 at 15:08

0 Answers0