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.
Python3
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"]) cfile |
Output:
“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
Python3
# find firstname starting with 'D' result = cfile.FirstName.str.startswith('D') print(result) # find lasttname containing 'XX' result = cfile.LastName.str.contains('XX') print(result) # split FirstName on the basis of ' ' result = cfile.FirstName.str.split() print(result) # find length of lasttname result = cfile.LastName.str.len() print(result) # Capitalize the first Letter of LastName result = cfile.LastName.str.capitalize() print(result) # Capitalize all Letter of LastName result = cfile.LastName.str.upper() print(result) # Convert all Letter of LastName to lowercase result = cfile.LastName.str.lower() print(result) |
Output:
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

