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 PandasFirst 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 PandasTo 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 PandasNow 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.
print(df['Names'].str.lower())
Output:
print(df['Names'].str.upper())
Output:
print(df['Names'].str.strip())
Output:
df['Split_Names'] = df['Names'].str.split('a')
print(df[['Names', 'Split_Names']])
Output:
print(df['Names'].str.len())
Output:
print(df)
print("\nafter using cat:")
print(df['Names'].str.cat(sep=', '))
Output:
print(df['City'].str.get_dummies())
Output:
print(df['Names'].str.startswith('G'))
Output:
print(df['Names'].str.endswith('h'))
Output:
print(df['Names'].str.replace('Gulshan', 'Gaurav'))
Output:
print(df['Names'].str.repeat(2))
Output:
print(df['Names'].str.count('a'))
Output:
print(df['Names'].str.find('a'))
Output:
print(df['Names'].str.findall('a'))
Output:
print(df['Names'].str.islower())
Output:
print(df['Names'].str.isupper())
Output:
print(df['Names'].str.isnumeric())
Output:
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