Python | Pandas Series.str.isdigit()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas str.isdigit() method is used to check if all characters in each string in series are digits. Whitespace or any other character occurrence in the string would return false. If the number is in decimal, then also false will be returned since this is a string method and ‘.’ is a special character and not a decimal in strings.

Syntax: Series.str.isdigit()



Return Type: Boolean series, Null values might be included too depending upon caller series.

To download the CSV used in code, click here.

In the following examples, the data frame used contains data on some NBA players. The image of data frame before any operations is attached below.

Example:
In this example, .isdigit() method is applied on the Age column. Before doing any operations, null rows are removed using .dropna() to avoid errors.
Since the Age column is imported as Float dtype, it is first converted into string using .astype() method. After that the isdigit() is applied twice, first on the original series and after that ‘.’ is removed using str.replace() method to see the output after removing special characters.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module
import pandas as pd
  
# making data frame
  
# removing null values to avoid errors
data.dropna(inplace = True)
  
# converting dtype to string
data["Age"]= data["Age"].astype(str)
  
# removing '.'
data["Age new"]= data["Age"].str.replace(".", "")
  
# creating bool series with original column
data["bool_series1"]= data["Age"].str.isdigit()
  
# creating bool series with new column
data["bool_series2"]= data["Age new"].str.isdigit()
  
# display
data.head(10)

chevron_right


Output:
As shown in output image, the Boolean series was false until decimal was present in string. After removing it, the new series has True for all values.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai