# Whitespace Woes: Using isspace() Method for Data Cleaning and Analysis

Python and NumPy are two of the most fundamental programming languages for data manipulation and analysis, and their libraries are widely used in data science and machine learning. Both libraries offer their own unique functions that make the life of a programmer much easier.

In this article, well explore the string isspace() method in Python and the isspace() method in NumPy.

## 1) Python String isspace() Method

The isspace() method in Python is used to check whether a string contains only whitespaces or not. As the name suggests, it checks each character in the string and returns True if all the characters are whitespaces.

If there is no whitespace in the given string, the method returns False. Syntax:

str.isspace()

## Where:

str – The input string whose characters need to be checked for whitespaces

## Example 1:

Consider a string “Tech Grammer” that has no whitespaces in it.

We can check if it contains a whitespace or not using the isspace() method as shown below:

string = “TechGrammer”

print(string.isspace())

False

## Example 2:

Now consider a string “Tech Grammer” that has a whitespace in it. We can again check if it contains a whitespace or not using the isspace() method as shown below:

string = “Tech Grammer”

print(string.isspace())

## Output:

False

As you can see from the output, the isspace() method returns False even though the string has a whitespace in it.

This is because the method checks whether all the characters in the given string are whitespace or not, and in this case, there is at least one character thats not a whitespace. Example 3:

Now let’s define a string that contains only whitespace characters.

string = ” “

print(string.isspace())

## Output:

True

As you can see from the output, the isspace() method returns True because all the characters in the given string are whitespaces.

## 2) NumPy isspace() Method

The NumPy isspace() method is a part of the NumPy library that is used to check whether the input elements of an array contain whitespace or not. The method checks each element in the array and returns a boolean array of the same shape as the input array.

## Syntax:

numpy.char.isspace(arr)

## Where:

numpy – The NumPy library

char – The sub-library within NumPy that deals with character manipulation

arr – The input array whose elements need to be checked for whitespace

## Example:

Let’s consider three different input arrays, each containing different types of characters including whitespaces. import numpy as np

inp_arr1 = np.array([‘Tech Grammer’, ‘Data Science Club’, ‘Artificial Intelligence’])

inp_arr2 = np.array([‘TechGrammer’, ‘DataScienceClub’, ‘ArtificialIntelligence’])

inp_arr3 = np.array([‘Tech!Grammer’, ‘Data’, ‘ArtificialIntelligence’])

We can now apply the isspace() method to these arrays and verify if the input elements contain whitespaces or not.

res1 = np.char.isspace(inp_arr1)

res2 = np.char.isspace(inp_arr2)

res3 = np.char.isspace(inp_arr3)

The results of the isspace() method applied to each of the above input arrays is given below. print(res1)

print(res2)

print(res3)

## Output:

[[False False False False False False True False False False False False]

[False False False False False False False False False False False False]

[False False False False False False False False False False False False]]

[[False False False False False False False False False False False False]

[False False False False False False False False False False False False]

[False False False False False False False False False False False False]]

[[False False False False False False False False False]

[False False False False False False False False False]

[False False False False False False False False False False]]

In the first two arrays, there are spaces between the words, so the method returned False as expected.

In the third array, there are not only spaces but also a special character, so the result is False for all the elements.

## Conclusion

In conclusion, the isspace() method in Python string and NumPy library can be used to check whether the input string or array elements contain only whitespaces or not. These methods return boolean values, True or False, for each character or element.

These functions can be helpful while performing data cleaning and manipulation. Pandas is another popular library used extensively in data science and machine learning.

It offers many functionalities that make data analysis and manipulation easier. In this section, we will discuss the Pandas isspace() method, which is similar to the isspace() method in Python and NumPy.

## 3) Pandas isspace() Method

The isspace() method in Pandas is used to check if a string contains only white spaces or not. It can work on both a single string and a whole column of the DataFrame.

If the values in the input data contain only white spaces, then this method returns True. Else, it returns False.

## Syntax:

Series/DataFrame.str.isspace()

## Where:

Series – A one-dimensional labeled array that holds a single column of data. DataFrame – A two-dimensional labeled data structure that holds multiple columns of data.

str – A Pandas string accessor that is used to access the string values of the Series or DataFrame. Example:

Let’s first define a Pandas DataFrame which contains strings with or without white spaces.

import pandas as pd

inp_data = {‘Name’: [‘Tech Grammer’, ‘John ‘, ‘ ‘, ‘Jack’, ‘N/A’]}

df = pd.DataFrame(inp_data)

print(df)

## Output:

Name

0 Tech Grammer

1 John

2

3 Jack

4 N/A

Now, let’s apply the isspace() method to the ‘Name’ column of our DataFrame. res = df[‘Name’].str.isspace()

print(res)

## Output:

0 False

1 False

2 True

3 False

4 False

Name: Name, dtype: bool

As you can see from the output, the isspace() method successfully returned a Pandas Series of boolean values indicating whether the input strings contain only white spaces.

In this case, the third row of our DataFrame that has an empty string, returned True. We can also apply the isspace() method to an individual string value instead of a whole column.

## For example:

str = ” Hello World “

res = str.isspace()

print(res)

## Output:

False

As expected, this method returns False since the string value contains space characters along with the non-whitespace characters.

## Applications of isspace() method

One practical application of the isspace() method is data cleaning. In many datasets, we encounter missing or incomplete data in the form of empty strings or strings with only white spaces.

These empty strings or white spaces can affect our data analysis and prediction models. By using the isspace() method, we can quickly determine if a specific string value or a column of the data contains only white spaces or empty strings.

Based on this information, we can take the necessary actions like replacing the empty strings with a user-defined value or removing the entire row/column of data if it contains no valuable information. Another application is in text and sentiment analysis, where analyzing texts and identifying important keywords is a critical step.

During preprocessing, the isspace() method can be used to identify and remove white space strings, which have no value in our analysis. This can help in making our sentiment analysis more accurate by eliminating unwanted words and whitespace.

## Conclusion

In conclusion, the isspace() method in Pandas can be an effective tool in identifying white space strings and empty strings in our dataset. By using this method, we can filter out the unwanted data values and help create more accurate and efficient analyses.

By understanding the isspace() method, we can unlock even more potential for Pandas, making it a powerful tool for data analysis and manipulation. In this article, we explored the isspace() method in three important libraries – Python, NumPy and Pandas.

We saw that isspace() method is used to check whether the input string or array contains only whitespace characters or not. Both Python and NumPy isspace() methods return a boolean value indicating whether the string or array elements contain only whitespaces or not.

Pandas isspace() method can be used to apply this check on a DataFrame column, aiding in data cleaning and preprocessing. Understanding and using isspace() method helps to filter out unwanted data values, which can result in more accurate and efficient data analysis.

Overall, isspace() method is a small but important function that can have a huge impact on data cleaning and analysis.