Get last "column" after .str.split() operation on column in pandas DataFrame
pandas, python, split, string
Solution
Do this:
In [43]: temp2.str[-1]
Out[43]:
0 p500
1 p600
2 p700
Name: ticker
So all together it would be:
>>> temp = pd.DataFrame({'ticker' : ['spx 5/25/2001 p500', 'spx 5/25/2001 p600', 'spx 5/25/2001 p700']})
>>> temp['ticker'].str.split(' ').str[-1]
0 p500
1 p600
2 p700
Name: ticker, dtype: object
Problem
I have a column in a pandas DataFrame that I would like to split on a single space. The splitting is simple enough with `DataFrame.str.split(' ')`, but I can't make a new column from the last entry. When I `.str.split()` the column I get a list of arrays and I don't know how to manipulate this to get a new column for my DataFrame. Here is an example. Each entry in the column contains 'symbol data price' and I would like to split off the price (and eventually remove the "p"... or "c" in half the cases). ``` import pandas as pd temp = pd.DataFrame({'ticker' : ['spx 5/25/2001 p500', 'spx 5/25/2001 p600', 'spx 5/25/2001 p700']}) temp2 = temp.ticker.str.split(' ') ``` which yields ``` 0 ['spx', '5/25/2001', 'p500'] 1 ['spx', '5/25/2001', 'p600'] 2 ['spx', '5/25/2001', 'p700'] ``` But `temp2[0]` just gives one list entry's array and `temp2[:][-1]` fails. How can I convert the last entry in each array to a new column? Thanks!