rokman54 rokman54 - 10 months ago 86
Python Question

Extract first characters from list series pandas

I have a string series containing multiples words. I want to extract the first character of each word per row in a vectorized fashion.

So far, I have been able to split the words into a list, but haven't found a vectorized way of getting the first characters.

s = pd.Series(['aa bb cc', 'cc dd ee', 'ff ga', '0w'])

>>> s. str.split()
0 [aa, bb, cc]
1 [cc, dd, ee]
2 [ff, ga]
3 [0w]

Eventually, I want something like this:

0 [a, b, c]
1 [c, d, e]
2 [f, g]
3 [0]


A straightforward way would be to use the method:

In [15]: s.str.split().map(lambda lst : [string[0] for string  in lst])
0    [a, b, c]
1    [c, d, e]
2       [f, g]
3          [0]
dtype: object