A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/string-manipulations-in-pandas-dataframe/ below:

String manipulations in Pandas DataFrame

String manipulations in Pandas DataFrame

Last Updated : 15 Jul, 2025

String manipulation is the process of changing, parsing, splicing, pasting or analyzing strings. As we know that sometimes data in the string is not suitable for manipulating the analysis or get a description of the data. But Python is known for its ability to manipulate strings. In this article we will understand how Pandas provides us the ways to manipulate to modify and process string data-frame using some builtin functions.

Create a String Dataframe using Pandas

First of all we will know ways to create a string dataframe using Pandas.

Python
import pandas as pd
import numpy as np

data = {'Names': ['Gulshan', 'Shashank', 'Bablu', 'Abhishek', 'Anand', np.nan, 'Pratap'],
        'City': ['Delhi', 'Mumbai', 'Kolkata', 'Delhi', 'Chennai', 'Bangalore', 'Hyderabad']}

df = pd.DataFrame(data)
print(df)

Output:

Change Column Datatype in Pandas

To change the type of the created dataframe to string type. we can do this with the help of .astype() . Let's have a look at them in the below example

Python
print(df.astype('string'))

Output:

String Manipulations in Pandas

Now we see the string manipulations inside a Pandas Dataframe, so first create a Dataframe and manipulate all string operations on this single data frame below so that everyone can get to know about it easily.

Example:

Python
import pandas as pd
import numpy as np

data = {'Names': ['Gulshan', 'Shashank', 'Bablu', 'Abhishek', 'Anand', np.nan, 'Pratap'],
        'City': ['Delhi', 'Mumbai', 'Kolkata', 'Delhi', 'Chennai', 'Bangalore', 'Hyderabad']}

df = pd.DataFrame(data)
print(df)

Output:

Let's have a look at various methods provided by this library for string manipulations.

Python
print(df['Names'].str.lower())

Output:

Python
print(df['Names'].str.upper())

Output:

Python
print(df['Names'].str.strip())

Output:

Python
df['Split_Names'] = df['Names'].str.split('a')
print(df[['Names', 'Split_Names']])

Output:

Python
print(df['Names'].str.len())

Output:

Python
print(df)

print("\nafter using cat:")
print(df['Names'].str.cat(sep=', ')) 

Output:

Python
print(df['City'].str.get_dummies())

Output:

Python
print(df['Names'].str.startswith('G'))

Output:

Python
print(df['Names'].str.endswith('h'))

Output:

Python
print(df['Names'].str.replace('Gulshan', 'Gaurav'))

Output:

Python
print(df['Names'].str.repeat(2))

Output:

Python
print(df['Names'].str.count('a'))

Output:

Python
print(df['Names'].str.find('a'))

Output:

Python
print(df['Names'].str.findall('a'))

Output:

Python
print(df['Names'].str.islower())

Output:

Python
print(df['Names'].str.isupper())

Output:

Python
print(df['Names'].str.isnumeric())

Output:

Python
print(df['Names'].str.swapcase())

Output:


Pandas & Strings in Python


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4