How do you replace multiple values in a list python?

In this article, we will learn to convert a list to a string in Python. We will use some built-in functions and some custom code as well. Let's first have a quick look over what is list and string in Python.

Python String

The String is a type in python language just like integer, float, boolean, etc. Data surrounded by single quotes or double quotes are said to be a string. A string is also known as a sequence of characters.

string1 = "apple"
string2 = "Preeti125"
string3 = "12345"
string4 = "Model number is 56@1"

Replace multiple Substrings in a String using replace[]

The below example uses replace[] function provided by Python Strings. It replaces all the occurrences of a sub-string to a new string by passing the old and new strings as parameters to the replace[] function. Multiple calls to replace[] function are required to replace multiple substrings in a string. There is also a third parameter that is optional in replace[] which accepts an integer to set the maximum count of replacements to execute.

Example 1

str1 = "Fear leads to anger; anger leads to hatred; hatred leads to conflict"

str2 = str1.replace["anger", "rage"]
str3 = str2.replace["to", "2"]

print[str3]


Fear leads 2 rage; rage leads 2 hatred; hatred leads 2 conflict

Example: Creates Dictionary of substrings to replace

The below examples create a dictionary of substrings as key-value pairs. Keys represent the sub-string and values are the new strings.

str1 = "Fear leads to anger; anger leads to hatred; hatred leads to conflict"

char_to_replace = {'to': '2', ';': ',', 'anger': 'rage'}

# Iterate over all key-value pairs in dictionary 
for key, value in char_to_replace.items[]:
    # Replace key character with value character in string
    str1 = str1.replace[key, value]

print[str1]


Fear leads 2 rage, rage leads 2 hatred, hatred leads 2 conflict

Example

This method uses two functions - translate[] and maketrans[].

maketrans[] - It a mapping table between the original string and its replacement. It is a method of str. It takes two parameters - an old string and a new string.

translate[] - It generates the translated string retuned by maketrans[] function.

import string

str1 = "Fear leads to anger; anger leads to hatred; hatred leads to conflict"

res = str1.translate[str.maketrans["o", "0"]]
print[res]


Fear leads t0 anger; anger leads t0 hatred; hatred leads t0 c0nflict

Example: Replace using Regex module

This method uses regex Python's module. Regex provides multiple string operations based on expressions. We will use two functions of the regex module- re.sub[] and re.subn[] to replace multiple substrings in a string.

sub[] - It replaces the contents of a string based on patterns. It takes a pattern or a string as the first argument. The second argument is the lambda function which extracts the matched sub-string then returns the value associated with it from the dictionary. It takes the original string as the third argument.

subn[] - It is similar to the above function but it returns a tuple of the converted string and the number of replacements made. It is useful if you want to note how many pattern groups you manipulated as metrics or for further processing.

Example 1 Use re.sub[]

When re.sub[] matches pattern from the string, it calls the lambda function, which gives the replacement character, then the sub[] function replaces that character in the string.

import re

str1 = "Fear leads to anger; anger leads to hatred; hatred leads to conflict"

str1 = re.sub["leads", lambda x: 'x', str1]

print[str1]


Fear x to anger; anger x to hatred; hatred x to conflict

Example : Use re.subn[]

import re

str1 = "Hi, my number is 08999XX. I am 23 years old. I live in 221B Baker Street."

str1, n = re.subn['[0-9]', 'X',str1]

print[str1]


Hi, my number is XXXXXXX. I am XX years old. I live in XXXB Baker Street.

Conclusion

In this article, we learned to replace multiple substrings in a string by using several built-in functions such as re.sub[], re.subn[], str.replace[], str.maketrans[], translate[] etc and we used some custom code as well.

In this Python Pandas tutorial, we will learn how to replace multiple values in Pandas DataFrame using Python. Also, we will cover these topics.

  • Pandas replace multiple values in a column based on condition
  • Pandas replace multiple values in column
  • Pandas replace multiple values with one value
  • Pandas replace multiple values regex
  • Pandas replace multiple values with nan
  • Pandas replace multiple values in one column
  • Pandas replace multiple row values
  • Pandas replace specific values in multiple columns
  • Pandas replace multiple values in multiple columns based on condition
  • Pandas replace multiple values at once
  • Pandas replace multiple characters at once
  • Pandas replace multiple string values
  • Pandas replace multiple characters in string
  • Pandas str.replace multiple values
  • Pandas remove multiple values from list
  • Pandas find and replace multiple values
  • In this Program, we will discuss how to replace multiple values in Pandas Python.
  • To replace multiple values in a DataFrame we can apply the method DataFrame.replace[]. In Pandas DataFrame replace method is used to replace values within a dataframe object.
  • In Python, we can use this technique to replace multiple columns and this method is also used for replacing a regex, dictionary, and series from the Pandas DataFrame.

Syntax:

Here is the Syntax of DataFrame.replace[] method

DataFrame.replace
                 [
                  to_replace=None,
                  value=None,
                  inplace=False,
                  limit=None,
                  regex=False,
                  method='pad'
                 ]
  • It consists of a few parameters
    • to_replace: This parameter specifies the value which we want to replace.
    • Value: By default its value is none and it specifies the new replacement value.
    • inplace: If the condition is True then it replaces and will return None. By default its value is False.
    • limit: It specifies the maximum size gap and by default its value is None.
    • regex: This parameter check the condition if it is true then to_replace must be string.
    • method: It is a method to use for replacement values.

Example:

Let’s take an example and check how to replace multiple values in DataFrame

import pandas as pd

new_data = pd.DataFrame[[
	[18,-78, 47],
	[19, 84, -92],
    [94, 73, 668],
    [809, 719, -356],
	[113,115,119]],
	columns=['m', 'o', 'p']]

new_result = new_data.replace[{'m':{94:19, 809:123}, 'p':{-92:63, -356:189}}]
print[new_result]

In the above program, we will use the replace[] method to replace the value in Dataframe. In this example, we will replace 94 with 19 and 809 with 123 in column ‘m’. Similarly, we will replace the value in column ‘p’.

Here is the execution of the following given code

Pandas replace multiple values

Read: Python Pandas Drop Rows

Pandas replace multiple values in a column based on condition

  • Let us see how to replace multiple values in a column based on condition.
  • By using DataFrame.loc method we can perform this particular task. In Pandas DataFrame the loc[] method is used to specify the name of the columns and rows that we need to filter out. So we can also filter the data by using the loc[] method.
  • It is a unique method and retrieves rows from a Pandas DataFramem and this method takes only index labels. When using the loc method on a Pandas DataFrame we have specified which rows and columns we want to adjust in the format.

Syntax:

DataFrame.loc[]

Example:

import pandas as pd

new_val = pd.DataFrame[[
	[89, 17, 15],
	[115, 178, -5],
	[114, 190, 824]],
	columns=['val1', 'val2', 'val3']]

new_val.loc[[new_val.val1 < 100], 'val1'] = 0
print[new_val]

In the above code, we have replaced those values in the column ‘val1′ which satisfies the condition. In this example, we have declared a condition if the column value is less than 100 then it will be replaced from DataFrame.

Here is the implementation of the following given code

Pandas replace multiple values in a column based on condition

By using NumPy.where function

In Python to replace values in columns based on condition, we can use the method numpy. where[]. In Python, this method will help the user to return the indices of elements from a numpy array after filtering based on a given condition.

Syntax:

Here is the Syntax of numpy.where[] method

numpy.where[condition[,x,y]]

Note: x and y parameter specifies the values from which to choose and always return the array with elements

Example:

import pandas as pd
import numpy as np

new_data = pd.DataFrame[[
	[18, 278, 897],
	[55, 77, 91],
	[678, 456, 118]],
	columns=['z', 'u', 't']]

new_data['u'] = np.where[[new_data.u < 200], 44, new_data.u]
print[new_data]

In the above code, we have used the numpy.where[] method and we have replaced those values in the column ‘u’ which satisfies the condition. In this example, we have declared a condition if the column value is less than 200 then it will be replaced by 44.

Here is the implementation of the following given code

Pandas replace multiple values in a column based on condition

Another approach to replace multiple values in a column based on condition by using DataFrame.where[] function.

In this method, we are going to specify the column name which values have to be replaced, and also we will replace the existing values in the given column based on the condition.

Syntax:

DataFrame.where
               [
                cond,
                other=nan,
                inplace=False,
                axis=None,
                Level=None,
                error='raise',
                try_cast=Nodefault.no_default
               ]

Source Code:

import pandas as pd

data = pd.DataFrame[[
	[76, 119,168],
	[346, 345, 156],
	[99, 145, 908]],
	columns=['Col1', 'Col2', 'Col3']]

data['Col3'].where[~[data.Col3 < 200], other=76, inplace=True]
print[data]

Here is the output of the following given code

Pandas replace multiple values in a column based on condition

Read: How to Convert Pandas DataFrame to a Dictionary

Pandas replace multiple values in column

  • Let us see how to replace multiple values in a specific column using Python Pandas.
  • In this example, we will see how to replace multiple values inside the column. To do this task we will use the Python inbuilt function DataFrame.replace.
  • By using DataFrame.replace[] method we will replace multiple values with multiple new strings or text for an individual DataFrame column. This method searches the entire Pandas DataFrame and replaces every specified value.

Source Code:

import pandas as pd                                                                                                                                

Country_name = pd.DataFrame[{'Country_name':['Germany', 'England', 'France', 'England', 'Germany']}]                                                                    
b= Country_name.replace[{'Country_name':{'England' : 'China', 'Germany' : 'japan' }}]                                                                                         

print[b]

In this program, we are going to specify the column name ‘Country_name’ which values have to be replaced, In the above code we have declared specific values which we want to replace with the new value.

Here is the Screenshot of the following given code

Pandas replace multiple values in column

Read: Pandas Delete Column

Pandas replace multiple values with one value

  • Here we can see how to replace multiple values with one value.
  • In this program, we will replace multiple strings with one specific string. To perform this particular task we can apply the combination of replacing and dict.fromkeys[] method.
  • In Python, the dict. fromkeys[] is a built-in function that declares a new dictionary with keys from the given sequence and values.

Syntax:

Here is the Syntax of fromkeys[] method

dict.fromkeys[seq[,value]]
  • It consists of few parameters
    • seq: This parameter specifies the list of all values.
    • Value: This is an optional paramter and by default its value is none.

Example:

import pandas as pd                                                                                                                                

Employee_name = pd.DataFrame[{'Employee_name':['john','george','Micheal','oliva']}]                                                                    
print[Employee_name]
b= Employee_name.replace[dict.fromkeys[['john','george','Micheal','oliva'], 'Micheal']]
print["After replacing the values:"]
print[b]

In the above program, we have replaced all the strings with the ‘Micheal’ string. Once you will print ‘b’ the output will display only ‘Micheal’ values in the DataFrame.

You can refer to the below Screenshot

Pandas replace multiple values with one value

Read: Python convert DataFrame to list

Pandas replace multiple values regex

  • In this program, we will discuss how to replace multiple values in Pandas DataFrame by using the regex method.
  • Here we can see how to replace the string that matches the regular expression. In Python to replace a string using regular expression using the replace[] method.
  • In Python, regular expressions are used to match character combinations in a given string and it also defines a search pattern. The Python module ‘re’ provides regular expression.

Syntax:

DataFrame.replace
                 [
                  to_replace=None,
                  value=None,
                  inplace=False,
                  limit=None,
                  regex=False,
                  method='pad'
                 ]

Source Code:

import pandas as pd                                                                                                                                

df = pd.DataFrame[{'City':['Ger-many', 'Eng-land', 'Fr-ance', 'Eng-land', 'Ger-many']}]  
new_value = {
   'CITY': {
      r'[G.*Ge|Germany.*]': 'Ger-many',
      r'E[ng]*[oo]*.*': 'Fraan ce'}
}

b= df.replace[new_value, regex=True, inplace=True]
print[b]

Here is the Screenshot of the following given code

Pandas replace multiple values regex

Read: Python Pandas DataFrame Iterrows

Pandas replace multiple values with nan

  • Here we can see how to replace multiple values with nan.
  • In this Program, we will understand how to replace values with nan values. To do this task we will apply the concept of DataFrame.replace[] method.
  • In Python nan stands for Not a Number and it represents the missing values in the DataFrame and these are the special values in Python.

Source Code:

import pandas as pd
import numpy as np

new_data = pd.DataFrame[[
	[76,889, 156],
	[19, 84, 467],
    [94, 73, 321],
    [116, 609,557],
	[156,864,467]],
	columns=['u', 'o', 'z']]

new_result = new_data.replace[{'o':{84:np.nan, 609:np.nan}, 'u':{94:np.nan, 156:np.nan}}]
print[new_result]

In the above program, we will use the replace[] method to replace the value in Dataframe. In this example, we will replace 84 with nan and 809 with nan in column ‘o’. Similarly, we will replace the value in column ‘u’.

Here is the execution of the following given code

Pandas replace multiple values with nan

Read: Count Rows in Pandas DataFrame

Pandas replace multiple values in one column

  • In this program, we will discuss how to replace multiple values in one specific column.
  • To perform this task we will use the method DataFrame.replace[] and check to replace[] method will solve this problem. In this example, we will replace ‘Country_name’ values with the new integer value under the ‘Country_name’ column.
  • In Pandas DataFrame replace method is used to replace values within a dataframe object.

Source Code:

import pandas as pd                                                                                                                                

Country_name = pd.DataFrame[{'Country_name':['China', 'Malaysia', 'Newzealand', 'Paris', 'Bangladesh']}]                                                                    
m= Country_name.replace[{'Country_name':{'Malaysia' : 56, 'Paris' : 778 }}]                                                                                         

print[m]

In the above program, we want to replace multiple values with multiple new values for an individual or specific Pandas DataFrame column.

Here is the output of the following given code.

Pandas replace multiple values in one column

Read: Python Pandas Write DataFrame to Excel

Pandas replace multiple row values

  • Let us see how to replace multiple row values in Pandas DataFrame.
  • By using DataFrame.loc method we can perform this particular task. In Pandas DataFrame the loc[] method is used to specify the name of the columns and rows that we need to filter out. So we can also filter the data by using the loc[] method.
  • In Python, this method retrieves rows from a Pandas DataFrame and it is also used with a boolean array.

Example:

import pandas as pd

new_data = pd.DataFrame[[
	[187, 634, 138],
	[779, 908, 346],
	[459, 709, 116]],
	columns=['new_val1', 'new_val2', 'new_val3']]

new_data.loc[[new_data.new_val2 < 800], 'new_val2'] = 92
print[new_data]

In the above code, we have replaced those values in the column ‘new_val2′ which satisfies the condition. In this example, we have declared a condition if the column value is less than 800 then it will be replaced from DataFrame.

You can refer to the below Screenshot

Pandas replace multiple row values

Read: Get index Pandas Python

Pandas replace specific values in multiple columns

  • In this program, we will discuss how to replace specific values in multiple columns.
  • In this example, the keys specify the column values and we want to replace specific values which is available in the DataFrame.
  • Here we can use the Pandas DataFrame.replace[] function to change or modify multiple column values. To do this task first create data by using sample function from the random module and then create two lists in which data has been stored.

Source Code:

import pandas as pd 
from random import sample

new_val = ["Rose", "Lilly","Jasmine","Lotus"]
my_val1 = sample[new_val,4]
my_val2 = sample[new_val,4]
my_val3 = sample[new_val,4]
df = pd.DataFrame[{"col1":my_val1,
              "col2":my_val2,
              "col3":my_val3,
             }]
print[df]
b= df.replace[{"Rose":"Banana",
            "Lilly":"Apple",
            "Jasmine":"Oranges",
            "Lotus":"Grapes"}]
print[b]

In the above example, we have created a DataFrame object and inserts three columns into it. Now we want to replace the values of all given specific columns.

Here is the implementation of the following given code

Pandas replace specific values in multiple columns

Read: Python DataFrame to CSV

Pandas replace multiple values in multiple columns based on condition

  • In this Program, we will discuss how to replace multiple values in multiple columns that are based on the condition.
  • By using Pandas.Series.map[] method we can solve this task. This method is used to map the values from two given series that have a specific column and the end column of the series should be the same as the index column.

Syntax:

Here is the Syntax of Pandas.Series.map[] method

Series.map[arg, na_action=None]

Example:

import pandas as pd 

new_df = pd.DataFrame[dict[
    new_val=['values {}'.format[m] for m in [8, 9, 6, 9, 8]],
    Other=range[5]
]]
print[new_df]
dct = {'value 1': 6789,
       'value 2': 95678,
       'value 3': 399456}
b= new_df.assign[new_val=new_df.new_val.map[dct]]
print[b]

Here is the Screenshot of the following given code

Pandas replace multiple values in multiple columns based on condition

Read: How to Set Column as Index in Python Pandas

Pandas replace multiple values at once

  • Let us see how to replace multiple values at once in a Pandas DataFrame.
  • To perform this task we can use the DataFrame.replace[] method for replacing multiple values at once.
  • In this example, we have created multiple columns ‘val1’ and ‘val2’ in which we have passed the string values and then created a DataFrame object and assign columns name in the list.

Source Code:

import pandas as pd

new_val = {'Val1':  ['Rose','Rose','Lilly','Jasmine','Jasmine','Rose','Lilly','Lilly'],
          'Val2': ['Australia','Germany','Germany','Australia','Germany','Germany','Australia','Australia']
         }

df = pd.DataFrame[new_val, columns= ['Val1','Val2']]
df['Val1'] = df['Val1'].replace[['Lilly'],'Lotus']
df['Val2'] = df['Val2'].replace[['Germany'],'China']

print [df]

In the above program, once you will print ‘df’ then the output will display ‘Lotus’ value instead of ‘Lilly’. Similarly in the second column, the ‘Germany’ value has been replaced with ‘China’.

Here is the Screenshot of the following given code

Pandas replace multiple values at once

Read: Groupby in Python Pandas

Pandas replace multiple string values

  • Here we can see how to replace multiple string values in Pandas DataFrame.
  • In Python string is a collection of characters and a character is simply a symbol and the string can be denoted within the single or double-quotes. Now we want to replace multiple strings values in a Pandas DataFrame.
  • To do this task we will use the concept of replace[] method. This method replaces all the occurrences of a string with a new string.

Example:

import pandas as pd                                                                                                                                

Fruit_name = pd.DataFrame[{'Fruit_name':['Mangoes', 'Apple', 'Grapes', 'Litchi', 'Cherry']}]                                                                    
m =Fruit_name.replace[{'Fruit_name':{'Apple' : 'Banana', 'Grapes' : 'Pears' }}]                                                                                         

print["Replace string values:",m]

In the above code, we want to replace multiple string values with multiple new string values for an individual DataFrame column. In this example, we will replace the ‘Apple’ string value with ‘Banana’.

You can refer to the below Screenshot

Pandas replace multiple string values

Read: Crosstab in Python Pandas

Pandas replace multiple characters in string

  • Here we can see how to replace multiple characters in a string by using DataFrame.
  • In this example, we will apply Python inbuilt functions that is replace[] and join[]. In Python, the join[] method is used to append a string with the iterable object and returns a new string by appending all of the elements which are available in the list.

Example:

import pandas as pd

df = pd.DataFrame[{'High_level': ['Python language', 'Java language', 'Ruby language', 'C++ language']}]

new_val = '|'.join[['Java', 'Ruby']]

df['High_level'] = df['High_level'].str.replace[new_val, 'Programming']
print[df]

In the above example, we have created a dataframe object ‘df’ and within this object, we have passed a key-value pair elements. In Pandas Dataframe the key element has been considered as a column name. In this program, we want to replace multiple characters in a given string. To do this task first we will create a list and assign replacing elements by using the join[] method.

Here is the implementation of the following given code

Pandas replace multiple characters in a string

Read: Python Pandas CSV Tutorial

Pandas str.replace multiple values

  • In this Program, we will discuss how to replace multiple values in a string by using the str.replace[] method.
  • Here we can use the replace[] method of the string datatype to replace value into a different value.
  • In Python str.replace[] method will help the user to replace matching elements of the old text in the given string replace with the new value.

Syntax:

str.replace[old,new [,count]]
  • It consists of few parameters
    • old: This parameter specifies character or text that should replaced from DataFrame.
    • new: A new string or text that will replace the old text.
    • Count: This is an optional parameter

Source Code:

import pandas as pd                                                                                                                                

Employee_name = pd.DataFrame[{'Employee_name':['Chris', 'Hayden', 'Potter', 'William', 'George']}]                                                                    
c= Employee_name.replace[{'Employee_name':{'Chris' : 'Hemsworth', 'Potter' : 'Adam' }}]                                                                                         

print[c]

In the above code first, we have imported the Pandas module and then create a dataframe object ‘Employee_name’. Now we have to use replace[] method to return a new string. Once you will print the ‘c’ then the output will display the new strings in the Pandas DataFrame.

You can refer to the below Screenshot

Pandas str replace multiple values

Read: Missing Data in Pandas in Python

Pandas replace multiple values from list

  • Let us see how to replace a multiple value from given list in Pandas DataFrame.
  • Here we can apply the concept of DataFrame.replace[] method and it will help the user to replace multiple values from list.

Source Code:

import pandas as pd
import numpy as np

new_data = pd.DataFrame[[
	[168,489, 136],
	[134, 378, 996],
    [152, 73, 321],
    [116, 609,557],
	[156,864,467]],
	columns=['l', 'm', 'n']]

new_result = new_data.replace[{'m':{378:960, 609:11}, 'n':{996:109, 557:338}}]
print[new_result]

In the above code, we have to use the replace[] method to replace the value in Dataframe. In this example, we will replace 378 with 960 and 609 with 11 in column ‘m’. Similarly, we will replace the value in column ‘n’.

Here is the Output of the following given code

Pandas replace multiple values from a list

Also, Read: Check If DataFrame is Empty in Python Pandas

Pandas find and replace multiple values

  • Here we can see how to find and replace multiple values in Pandas DataFrame.
  • By using np.where[] function we can perform this task and this method will help the user to return the indices of elements from a numpy array after filtering based on a given condition.

Example:

import pandas as pd
import numpy as np

new_val = pd.DataFrame[[
	[18, 278, 897],
	[55, 77, 91],
	[678, 456, 118]],
	columns=['l', 'o', 'p']]

new_val['o'] = np.where[[new_val.o < 200], 56, new_val.o]
print[new_val]

Here is the Screenshot of the following given code

Pandas find and replace multiple values

In this Pandas tutorial, we have learned how to replace multiple values in Pandas DataFrame using Python. Also, we have covered these topics.

  • Pandas replace multiple values in a column based on condition
  • Pandas replace multiple values in column
  • Pandas replace multiple values with one value
  • Pandas replace multiple values regex
  • Pandas replace multiple values with nan
  • Pandas replace multiple values in one column
  • Pandas replace multiple row values
  • Pandas replace specific values in multiple columns
  • Pandas replace multiple values in multiple columns based on condition
  • Pandas replace multiple values at once
  • Pandas replace multiple characters at once
  • Pandas replace multiple string values
  • Pandas replace multiple characters in string
  • Pandas str.replace multiple values
  • Pandas remove multiple values from list
  • Pandas find and replace multiple values

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

How do you replace all values in a list Python?

We can replace values inside the list using slicing. First, we find the index of variable that we want to replace and store it in variable 'i'. Then, we replace that item with a new value using list slicing.

How do you replace multiple elements in Python?

Method 2: Replace multiple characters using translate[] + maketrans[] There is also a dedication function that can perform this type of replacement task in a single line hence this is a recommended way to solve this particular problem.

How do you replace multiple characters in a list Python?

Use str.replace[] to Replace Multiple Characters in Python..
Use re.sub[] or re.subn[] to Replace Multiple Characters in Python..
translate[] and maketrans[] to Replace Multiple Characters in Python..

How do you replace multiple parts of a string in Python?

In Python, the String class [Str] provides a method replace[old, new] to replace the sub-strings in a string. It replaces all the occurrences of the old sub-string with the new sub-string.

Bài mới nhất

Chủ Đề