Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to Manipulate Strings in Pandas?

  • Last Updated : 05 Sep, 2020

Pandas Library provides multiple methods that can be used to manipulate string according to the required output. But first, let’s create a Pandas dataframe.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


import pandas as pd
data = [[1, "ABC KUMAR", "xYZ"], [2, "BCD", "XXY"],
        [3, "CDE KUMAR", "ZXX"], [3, "DEF", "xYZZ"]]
cfile = pd.DataFrame(data, columns = ["SN", "FirstName", "LastName"])


“Pandas” library provides a “.str()”  method that can be used to create any data of the data frame into a string,  After that any string operation defined in python documentation or in this article can be used on that data.

Below is  the code that illustrates some examples


# find firstname starting with 'D'
result = cfile.FirstName.str.startswith('D')
# find lasttname containing 'XX'
result = cfile.LastName.str.contains('XX')
# split FirstName on the basis of ' '
result = cfile.FirstName.str.split()
# find length of lasttname
result = cfile.LastName.str.len()
# Capitalize the first Letter of LastName
result = cfile.LastName.str.capitalize()
# Capitalize all Letter of LastName
result = cfile.LastName.str.upper()
# Convert all Letter of LastName to lowercase
result = cfile.LastName.str.lower()


0    False
1    False
2    False
3     True
Name: FirstName, dtype: bool
0    False
1     True
2     True
3    False
Name: LastName, dtype: bool
0    [ABC, KUMAR]
1           [BCD]
2    [CDE, KUMAR]
3           [DEF]
Name: FirstName, dtype: object
0    3
1    3
2    3
3    4
Name: LastName, dtype: int64
0     Xyz
1     Xxy
2     Zxx
3    Xyzz
Name: LastName, dtype: object
0     XYZ
1     XXY
2     ZXX
3    XYZZ
Name: LastName, dtype: object
0     xyz
1     xxy
2     zxx
3    xyzz
Name: LastName, dtype: object

My Personal Notes arrow_drop_up
Recommended Articles
Page :