Pandas: replace string with another string

Pandas: replace string with another string

In pandas, you can replace a specific string with another string in a DataFrame using the replace() method. Here's how you can do it:

import pandas as pd # Create a sample DataFrame data = {'Column1': ['apple', 'banana', 'apple', 'orange']} df = pd.DataFrame(data) # Replace 'apple' with 'fruit' df['Column1'] = df['Column1'].replace('apple', 'fruit') print(df) 

In this example, the replace() method is used to replace the string 'apple' with 'fruit' in the 'Column1' of the DataFrame. The result will look like this:

 Column1 0 fruit 1 banana 2 fruit 3 orange 

You can also use regular expressions and more complex replacement patterns with the replace() method. For example:

# Replace all strings containing 'fruit' with 'new_fruit' df['Column1'] = df['Column1'].replace('.*fruit.*', 'new_fruit', regex=True) 

This will replace any string containing the word 'fruit' with 'new_fruit'.

Remember that the replace() method returns a new Series or DataFrame with the replacements applied, so if you want to modify the original DataFrame, you need to assign the result back to the column.

Examples

  1. How to replace a string with another string in Pandas DataFrame column?

    Description: You can use the replace() function to replace occurrences of a string with another string in a specific column of a Pandas DataFrame.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apple', 'banana', 'orange']}) # Replace 'apple' with 'pear' in the 'Column' column df['Column'] = df['Column'].replace('apple', 'pear') print(df) 
  2. Pandas DataFrame: Replace substring in column with another string

    Description: You can use the str.replace() method to replace a substring in a column with another string.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apples are good', 'bananas are delicious', 'oranges are juicy']}) # Replace 'are' with 'are not' in the 'Column' column df['Column'] = df['Column'].str.replace('are', 'are not') print(df) 
  3. Pandas: Replace part of string in DataFrame column

    Description: Utilize the str.replace() method to replace part of a string in a column of a Pandas DataFrame with another string.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apple pie', 'banana split', 'orange juice']}) # Replace 'pie' with 'tart' in the 'Column' column df['Column'] = df['Column'].str.replace('pie', 'tart') print(df) 
  4. How to globally replace a string with another string in Pandas DataFrame?

    Description: Use the replace() function with the regex parameter set to True to globally replace a string with another string in all columns of a Pandas DataFrame.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column1': ['apple', 'banana', 'orange'], 'Column2': ['apple juice', 'banana smoothie', 'orange soda']}) # Globally replace 'apple' with 'pear' in all columns df = df.replace('apple', 'pear', regex=True) print(df) 
  5. Python Pandas: Replace entire string in DataFrame column

    Description: You can directly assign a new string to a column in a Pandas DataFrame to replace the entire string.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apple', 'banana', 'orange']}) # Replace entire column with 'fruit' df['Column'] = 'fruit' print(df) 
  6. Pandas DataFrame: Replace multiple strings in a column

    Description: Utilize the replace() function with a dictionary to replace multiple strings in a column of a Pandas DataFrame with corresponding replacement strings.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apple', 'banana', 'orange', 'grape']}) # Replace 'apple' with 'pear' and 'orange' with 'mandarin' in the 'Column' column df['Column'] = df['Column'].replace({'apple': 'pear', 'orange': 'mandarin'}) print(df) 
  7. Pandas: Replace string in DataFrame with NaN

    Description: Use the replace() function to replace a specific string in a column of a Pandas DataFrame with NaN (missing value).

    import pandas as pd import numpy as np # Sample DataFrame df = pd.DataFrame({'Column': ['apple', 'banana', 'orange', 'apple']}) # Replace 'apple' with NaN in the 'Column' column df['Column'] = df['Column'].replace('apple', np.nan) print(df) 
  8. Python Pandas: Replace string with empty string in DataFrame column

    Description: You can use the str.replace() method to replace a string with an empty string in a column of a Pandas DataFrame.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apple', 'banana', 'orange', 'apple']}) # Replace 'apple' with empty string in the 'Column' column df['Column'] = df['Column'].str.replace('apple', '') print(df) 
  9. Pandas DataFrame: Replace string based on condition

    Description: Use the loc accessor with boolean indexing to replace a string in a column of a Pandas DataFrame based on a condition.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apple', 'banana', 'orange', 'apple']}) # Replace 'apple' with 'pear' if it exists in the 'Column' column df.loc[df['Column'] == 'apple', 'Column'] = 'pear' print(df) 
  10. Pandas: Replace string in DataFrame column with regular expression

    Description: You can use the str.replace() method with regular expressions to replace strings in a column of a Pandas DataFrame.

    import pandas as pd # Sample DataFrame df = pd.DataFrame({'Column': ['apple', 'banana', 'orange']}) # Replace strings starting with 'a' with 'fruit' in the 'Column' column df['Column'] = df['Column'].str.replace('^a.*', 'fruit', regex=True) print(df) 

More Tags

rpgle asp.net-roles jenkins-declarative-pipeline collision-detection http-status-code-401 react-slick fieldset apache-tez marshalling outlook-redemption

More Python Questions

More Investment Calculators

More Bio laboratory Calculators

More Housing Building Calculators

More Mixtures and solutions Calculators