Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (17.6k points)

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!

1 Answer

0 votes
by (41.4k points)

You can use this below code:

In [43]: temp2.str[-1]

Out[43]: 

0    p500

1    p600

2    p700

Name: ticker

If you wish to Learn more about Pandas visit this Pandas Tutorial.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...