Saturday, December 28, 2024
Google search engine
HomeLanguagesTop 10 String methods in Pandas

Top 10 String methods in Pandas

Pandas is an open-source Python library that is mainly used for data manipulation and is widely popular in the fields of machine learning and data science. In this article, we will be learning various string methods that the Pandas library has got to offer. 

The Pandas library is very useful for the manipulation of strings as it provides us with various handy string methods. It saves time and makes our program efficient. Now let us get acquainted with various string methods that the Pandas library has got to offer.

  Method Description
1) upper() Converts a string into uppercase
2) lower() Converts a string into lowercase
3) isupper() Checks whether the character is uppercase or not
4) islower() Checks whether the character is lowercase or not
5) len() Identifies the length of the string.
6) startswith()  Returns true if the element starts with the pattern
7) split() Splits the string at a particular index or character
8) find() Returns the index at where the given string is found
9) strip() Strips whitespaces from each string from both sides.
10) replace()  Replaces a part of the string with another one.

Owing to the fact that you have understood the above string methods provided by the Pandas, let us now go ahead with a few examples that use the above methods.

We will be using the below data frame for the purpose of the illustration.

Python3




import pandas as pd
sports = pd.Series(['Virat', 'azam', 'fiNch', 'ShakiB', 'STOKES', 'KAne'])
print(sports)


Output:

0     Virat
1      azam
2     fiNch
3    ShakiB
4    STOKES
5      KAne
dtype: object

1.) DataFrame.upper()

Convert each string to upper case.

Python3




print("Upper Case:")
print(s.str.upper())


Output:

Upper Case:
0     VIRAT
1      AZAM
2     FINCH
3    SHAKIB
4    STOKES
5      KANE
dtype: object

2.) DataFrame.lower()

Convert each string to lowercase.

Python3




print("Lower Case:")
print(s.str.lower())


Output:

Lower Case:
0     virat
1      azam
2     finch
3    shakib
4    stokes
5      kane
dtype: object

3.) DataFrame.isupper()

It returns boolean values based on whether each character present in the string is in upper case or not.

Python3




print("Checks whether string is in Upper Case:")
print(s.str.isupper())


Output:

Checks whether string is in Upper Case:
0    False
1    False
2    False
3    False
4     True
5    False
dtype: bool

4.) DataFrame.islower()

It returns boolean values based on whether each character present in the string is in lowercase or not.

Python3




print("Checks whether string is in Lower Case:")
print(s.str.islower())


Output:

Checks whether string is in Lower Case:
0    False
1     True
2    False
3    False
4    False
5    False
dtype: bool

5.) DataFrame.len()

This function returns the length of each string.

Python3




print("Length of strings:")
print(s.str.len())


Output:

Length of strings:
0    5
1    4
2    5
3    6
4    6
5    4
dtype: int64

6.) DataFrame.startswith()

It returns boolean values based on whether the string starts with a certain character sequence or not.

Python3




print("Checks whether string starts with certain substring:")
print(s.str.startswith('a'))


Output:

Checks whether string is numeric:
0    False
1    False
2    False
3    False
4    False
5    False
dtype: bool

7.) DataFrame.split()

This function helps to split the string by a certain character or symbols at once.

Python3




print("Splits string by character 'a':")
print(s.str.split('a'))


Output:

Checks whether string is numeric:
0     [Vir, t]
1     [, z, m]
2      [fiNch]
3    [Sh, kiB]
4     [STOKES]
5       [KAne]
dtype: object

8.) DataFrame.find()

This function finds the index of the occurrence of a certain character sequence.

Python3




print("Find the index of the searched character or substring:")
print(s.str.find('a'))


Output:

Find the index of the searched character or substring:
0    3
1    0
2   -1
3    2
4   -1
5   -1
dtype: int64

9.) DataFrame.strip()

It helps to remove the extra trailing spaces from the start and the end.

Python3




print("Remove extra space from the starting and the end of the string:")
print(s.str.strip())


Output:

Remove extra space from the starting and the end of the string:
0     Virat
1      azam
2     fiNch
3    ShakiB
4    STOKES
5      KAne
dtype: object

10.) DataFrame.replace()

This function helps to remove certain character sequence sometimes which are present in all the strings and is undesired.

Python3




print("Replace a particular substring by desired pattern:")
print(s.str.replace('a', ''))


Output:

Replace a particular substring by desired pattern:
0      Virt
1        zm
2     fiNch
3     ShkiB
4    STOKES
5      KAne
dtype: object

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments