Keep elements with pattern in pandas series without converting them to list

  list, pandas, python, regex, series

I have the following dataframe:

df = pd.DataFrame(["type:1, kind:2, water", "something, blu:3, somethingelse"], columns = ['A'])

and I want to create a new column that contains for each row all the elements that have a ":" in them. So for example in the first row I want to return "type:1, kind:2" and for the second row I want "blu:3". I managed by using a list comprehension in the following way:

df['new'] = [[y for y in x  if ":" in y] for x in df['A'].str.split(",")]

But my issue is that the new column contains list elements.

    A                                new
0   type:1, kind:2, water            [type:1, kind:2]
1   something, blu:3, somethingelse  [ blu:3]

I have not used Python a lot so I am not 100% whether I am missing a more Pandas specific way to do this. If there is one, more than happy to learn about it and use it.
If this is a correct approach how can I convert the elements back into strings in order to do regexes on them? I tried How to concatenate items in a list to a single string? but this is not working as I would like it to.

Source: Python Questions

LEAVE A COMMENT