Search code examples
pythonarraysdatetimesplitseries

how to convert array in series to normal number in python


[ number of days - recency column enter image description here

I wish to extract the number of days only from the recency column

and i converted it to string and after split function extracted first part a["recency"].str[:1]

But now the issue is I am unable to work on this series as its not accepted as int and not letting me convert it to a number series or any series as I am getting errors after splitting data. error:

when I try to convert to int ValueError: setting an array element with a sequence .

And if i try to map to int and use it at series TypeError: object of type 'map' has no len()

some help would be great as I am still new to this.


Solution

  • First make sure that your data has no null values.

    For removing null values you can use

    a.dropna(inplace=True)

    Assuming a is your dataframe

    Now extract the first word from each row of recency column into elements variable

    elements=a["recency"]
    elements=[element.split(" ")[0] for element in elements]
    

    Finally append all the numbers into the list

    nums=[]
    error_in_datas=[]
    for x in range(len(elements)):
      if elements[x].isdigit():
        nums.append(int(elements[x]))
      else:
        error_in_datas.append(x)
    print(nums)
    print(error_in_datas)
    

    nums is the list of your days and error_in_datas contains list of indexes that has no digits in the first place of your data as you wanted