How to replace a string in a list of strings in a DataFrame (Python)?

pandas dataframe replace string in column
pandas replace specific values in column
pandas replace part of string
pandas replace values in column based on condition
pandas replace multiple values
pandas replace values in column based on multiple condition
pandas replace regex
pandas replace string with number

I have a Dataframe which consists of lists of lists in two seperate columns.

import pandas as pd
data = pd.DataFrame()
data["Website"] = [["google.com", "amazon.com"], ["google.com"], ["aol.com", "no website"]]
data["App"] = [["Ok Google", "Alexa"], ["Ok Google"], ["AOL App", "Generic Device"]]

Thats how the Dataframe looks like

I need to replace certain strings in the first column (here: "no website") with the according string in the second column (here: "Generic Device"). The replacing string has the same index in the list as the string that needs to be replaced.

What did not work so far: I tried several forms of str.replace(x,y) for lists and DataFrames and nothing worked. A simple replace(x,y) does not work as I need to replace several different strings. I think I can't get my head around the indexing thing. I already googled and stackoverflowed for two hours and haven't found a solution yet.

Many thanks in advance! Sorry for bad engrish or noob mistakes, I am still learning.

-Max

Define replacement function and use apply to vectorize

def replacements(websites, apps):
    " Substitute items in list replace_items that's found in websites "
    replace_items = ["no website", ] # can add to this list of keys 
                                     # that trigger replacement

    for i, k in enumerate(websites):
        # Check each item in website for replacement
        if k in replace_items:
            # This is an item to be replaced
            websites[i] = apps[i]  # replace with corresponding item in apps

    return websites

# Create Dataframe
websites = [["google.com", "amazon.com"], ["google.com"], ["aol.com", "no website"]]
app = [["Ok Google", "Alexa"], ["Ok Google"], ["AOL App", "Generic Device"]]
data = list(zip(websites, app))
df = pd.DataFrame(data, columns = ['Websites', 'App'])

# Perform replacement
df['Websites'] = df.apply(lambda row: replacements(row['Websites'], row['App']), axis=1)
print(df)

Output

                   Websites                        App
0   [google.com, amazon.com]         [Ok Google, Alexa]
1               [google.com]                [Ok Google]
2  [aol.com, Generic Device]  [AOL App, Generic Device]

Python, n: Number of replacement to make in a single string, default is -1 which In the following examples, the data frame used contains data of some� Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.replace() function is used to replace a string, regex, list, dictionary, series, number etc. from a dataframe. This is a very rich function as it has many variations.

Try this,You can define replaceable values in a array and execute.

def f(x,items):
    for rep in items:
        if rep in list(x.Website):
            x.Website[list(x.Website).index(rep)]=list(x.App)[list(x.Website).index(rep)]    
    return x

items = ["no website"]
data = data.apply(lambda x: f(x,items),axis=1)

Output:

                     Website                        App
0   [google.com, amazon.com]         [Ok Google, Alexa]
1               [google.com]                [Ok Google]
2  [aol.com, Generic Device]  [AOL App, Generic Device]

pandas.DataFrame.replace — pandas 1.1.0 documentation, str: string exactly matching to_replace will be replaced with value Second, if regex=True then all of the strings in both lists will be interpreted as regexs� Equivalent to str.replace() or re.sub(), depending on the regex value. Parameters pat str or compiled regex. String can be a character sequence or regular expression. repl str or callable. Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. See re.sub(). n int, default -1 (all)

First of all, Happy Holidays!

I wasn't really sure what your expected output was and I'm not really sure what you have tried previously, but I think that this may work:

data["Website"] = data["Website"].replace("no website", "Generic Device")

I really hope this helps!

pandas.Series.str.split — pandas 1.1.0 documentation, If False , return Series/Index, containing lists of strings. Returns. Series, Index, DataFrame or MultiIndex. Type matches caller unless expand=True (see� I have a very large dataset were I want to replace strings with numbers. I would like to operate on the dataset without typing a mapping function for each key (column) in the dataset. (similar to the fillna method, but replace specific string with assosiated value).

You can create a function like this:

def f(replaced_value, col1, col2):
    def r(s):
        while replaced_value in s[col1]:
            s[col1][s[col1].index(replaced_value)] = s[col2][s[col1].index(replaced_value)]
        return s
    return r

and use apply:

df=df.apply(f("no website","Website","App"), axis=1)
print(df)

Working with text data — pandas 1.1.0 documentation, Currently, the performance of object dtype arrays of strings and arrays. For backwards-compatibility, object dtype remains the default type we infer a list of strings to Or astype after the Series or DataFrame is created If you do want literal replacement of a string (equivalent to str.replace() ), you can set the optional regex� Replace the two first occurrence of the word "one": txt = "one one was a race horse, two two was one too." x = txt.replace ("one", "three", 2)

Python Strings: Replace, Join, Split, Reverse, Uppercase & Lowercase, var = "Hello World!" In this tutorial, we will learn -. Accessing Values in Strings; Various String Operators; Some more examples; Python String� Before calling .replace() on a Pandas series, .str has to be prefixed in order to differentiate it from the Python’s default replace method. Syntax: Series.str.replace(pat, repl, n=-1, case=None, regex=True) Parameters: pat: string or compiled regex to be replaced repl: string or callabe to replace instead of pat

Python Replace Comma With Space In List, Python: Replacing strings in a list with values from a dictionary. In this tutorial, we will learn how to split a string by comma , in Python using String. GroupBy Value in DataFrame and getting a list of words seperated by comma Hot but maybe a comma if converting to a CSV file)? Ideally, the Python program would not� If this is True then to_replace must be a string. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which case to_replace must be None. method {‘pad’, ‘ffill’, ‘bfill’, None} The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None.

How can I replace values with 'none' in a dataframe using pandas , How can I replace values with 'none' in a dataframe using pandas. +2 votes You can do it by passing either a list or a dictionary: In [11]: How can I write code to find a palindrome in python without using string functions? The 'string' extension type solves several issues with object-dtype NumPy arrays: 1) You can accidentally store a mixture of strings and non-strings in an object dtype array. A StringArray can only store strings. 2) object dtype breaks dtype-specific operations like DataFrame.select_dtypes().

Comments
  • hey Max, are you able to show your intended output?
  • @all: Thank you very much! All your codes worked on my sample data, but not when I integrate it into my own script. I have to find out why, but that's beyond the scope of this question.