-2

consider my Dataframe

       calories  duration
  0       420        50.55
  1       380        40.48
  2       390        food

I need to ensure that the duration doesn't contain character i tried

if df["duration"].isdigit():
    print("True")
else:
    print('The input is not a valid number')

but it gives error:

'Series' object has no attribute 'isdigit'
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Nidhi
  • 9
  • 1
  • 1
  • 1
    You probably need to do this for each [value in the series](https://pandas.pydata.org/docs/reference/api/pandas.Series.values.html#pandas.Series.values). – jarmod May 16 '22 at 19:35
  • What does `"50.55".isdigit()` return? For a solution to this, see [this answer](https://stackoverflow.com/a/20929881/15261315). – Chris May 16 '22 at 19:42

1 Answers1

1

You should use .str.isdigit() on you Series object. it gives you a True or False value for each element in the Series. Then you should check that all generated values are True. You can do that by calling .all() on the resulting series.

If your duration column is of type str, this will give you the answer.

df['duration'].str.isdigit().all()
Nima Afshar
  • 486
  • 2
  • 7