Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

เขียนโปรแกรม Python ตรวจสอบสมาชิกที่ดัชนี k เป็นตัวอักษรหรือตัวเลขใน Series


อินพุต - สมมติคุณมี Series

a    abc b    123 c    xyz d    ijk

วิธีแก้ปัญหา

ทำตามขั้นตอนด้านล่าง

  • สร้าง Series

  • รับดัชนีจากผู้ใช้

  • ตรวจสอบด้วย isdigit()

if(data[x].isdigit()):
    print("digits present")
else:
    print("not present")

ตัวอย่าง

import pandas as pd
dic = {'a':'abc','b':'123','c':'xyz','d':'ijk'}
data = pd.Series(dic)
x = input("enter the index : ")
if(data[x].isdigit()):
    print("digits present")
else:
    print("not present")

ผลลัพธ์

enter the index : a not present enter the index : b digits present