I have the following dataframe:
df = pd.DataFrame(["Air type:1, Space kind:2, water", "something, Space 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 Air type:1, Space kind:2, water [Air type:1, Space kind:2]
1 something at the start:4, Space blu:3, somethingelse [something at the start:4, Space 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.