Home > Back-end >  I have a series but I want to split to the right of a comma to get the integer
I have a series but I want to split to the right of a comma to get the integer

Time:04-01

For example I have this

1   ['cancelled:', '7']

And I want to split to only get the integer value of 7. How can I go about this using str.split()?

CodePudding user response:

The split method returns a list. To get the second element of this list, the number, do str.split(sep)[1], where "sep" is the separator you use.

If your list contains various couples of elements like ["cancelled:", "7", "accepted", "19"], you can get all the elements of the list with uneven index, using a range(1, len(list), 2):

your_list = string.split(sep)
your_list = [your_list[i] for i in range(1, len(your_list), 2)]

CodePudding user response:

You can use s.str.split(expand=True):

import pandas as pd

s = pd.Series(['cancelled:', '7'])
a = s.str.split(expand = True)
print(int(a[0][1]))

> Output:
> 7
  • Related