Write a program in Python to verify kth index element is either alphabet or number in a given series

In this tutorial, we'll learn how to verify whether the kth index element in a Pandas Series contains alphabetic characters or numeric digits. This is useful for data validation and type checking operations.

Input − Assume, you have a Series:

a    abc
b    123
c    xyz
d    ijk

Solution

To solve this, we will follow the steps given below −

  • Define a Series with mixed data types

  • Get the index from user input

  • Use string methods to check if the value contains digits or alphabets

  • Display appropriate messages based on the content type

Using isdigit() Method

The isdigit() method returns True if all characters in the string are digits ?

import pandas as pd

# Create a Series with mixed content
dic = {'a': 'abc', 'b': '123', 'c': 'xyz', 'd': 'ijk'}
data = pd.Series(dic)

print("Series data:")
print(data)
print()

# Check specific indices
test_indices = ['a', 'b', 'c', 'd']

for idx in test_indices:
    if data[idx].isdigit():
        print(f"Index '{idx}': digits present")
    else:
        print(f"Index '{idx}': not digits")
Series data:
a    abc
b    123
c    xyz
d    ijk
dtype: object

Index 'a': not digits
Index 'b': digits present
Index 'c': not digits
Index 'd': not digits

Using isalpha() Method

The isalpha() method returns True if all characters in the string are alphabetic ?

import pandas as pd

dic = {'a': 'abc', 'b': '123', 'c': 'xyz', 'd': 'ijk'}
data = pd.Series(dic)

test_indices = ['a', 'b', 'c', 'd']

for idx in test_indices:
    if data[idx].isalpha():
        print(f"Index '{idx}': alphabets present")
    else:
        print(f"Index '{idx}': not alphabets")
Index 'a': alphabets present
Index 'b': not alphabets
Index 'c': alphabets present
Index 'd': alphabets present

Complete Verification Function

Here's a comprehensive function that checks both digits and alphabets ?

import pandas as pd

def verify_content_type(series, index):
    """Verify if the content at given index is alphabet or number"""
    try:
        value = series[index]
        if value.isdigit():
            return f"Index '{index}': Contains digits only"
        elif value.isalpha():
            return f"Index '{index}': Contains alphabets only"
        elif value.isalnum():
            return f"Index '{index}': Contains alphanumeric characters"
        else:
            return f"Index '{index}': Contains special characters or spaces"
    except KeyError:
        return f"Index '{index}': Not found in series"

# Create test series
dic = {'a': 'abc', 'b': '123', 'c': 'xyz123', 'd': 'hello world', 'e': '456'}
data = pd.Series(dic)

print("Series content:")
print(data)
print()

# Test different indices
for idx in ['a', 'b', 'c', 'd', 'e']:
    print(verify_content_type(data, idx))
Series content:
a          abc
b          123
c       xyz123
d    hello world
e          456
dtype: object

Index 'a': Contains alphabets only
Index 'b': Contains digits only
Index 'c': Contains alphanumeric characters
Index 'd': Contains special characters or spaces
Index 'e': Contains digits only

Comparison

Method Purpose Returns True When
isdigit() Check for digits All characters are digits (0-9)
isalpha() Check for alphabets All characters are letters (a-z, A-Z)
isalnum() Check for alphanumeric All characters are letters or digits

Conclusion

Use isdigit() to verify if a Series element contains only numeric digits, and isalpha() to check for alphabetic characters. Combine these methods for comprehensive data validation in Pandas Series operations.

Updated on: 2026-03-25T15:50:04+05:30

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements