How to do STRING munging in Pandas DataFrame in Python

How to do STRING munging in Pandas DataFrame in Python

String munging, also known as data cleaning or data wrangling, is the process of modifying and manipulating string values within a Pandas DataFrame in Python.

First, you need to import the Pandas library and create a DataFrame. For example, you can create a DataFrame with some sample data that contains string values.

import pandas as pd

data = {'product': ['Apple', 'Banana', 'Cherry', 'Date', 'Eggplant'],

'product_code': ['A1001', 'B1002', 'C1003', 'D1004', 'E1005'],

'description': ['Red Delicious', 'Cavendish', 'Bing', 'Medjool', 'Black Beauty']}

df = pd.DataFrame(data)

Next, you can use various string munging techniques to modify and manipulate the string values in the DataFrame.

For example, you can use the upper() or lower() function to convert the strings to uppercase or lowercase respectively:

df['product'] = df['product'].str.upper() 
df['description'] = df['description'].str.lower()

You can also use the len() function to determine the length of each string:

df['description_length'] = df['description'].str.len()

You can also use the replace() function to replace specific characters or substrings within a string:

df['product_code'] = df['product_code'].str.replace('1', '2')

You can also use the startswith() or endswith() function to check whether a string starts or ends with a specific character or substring:

df['starts_with_A'] = df['product'].str.startswith('A') 
df['ends_with_t'] = df['product'].str.endswith('t')

By using these string munging techniques, you can easily modify and manipulate string values within a Pandas DataFrame in Python. This can be useful for data cleaning and preprocessing, as it allows you to standardize the data and make it easier to work with.

 

In this Learn through Codes example, you will learn: How to do STRING munging in Pandas DataFrame in Python.



Find more … …

Python Strings – Python String count() with EXAMPLES

How to format STRING in a Pandas DataFrame in Python

Python Strings – Python String find() Method with Examples

Python Example – Write a Python program to count the number of strings