Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Get Last Element after str.split()

I use pandas and I have data and the data look like this

FirstName LastName StudentID
FirstName2 LastName2 StudentID2

Then I split it based on 'space' using str.split()

So the data will look like this in DataFrame

[[FirstName, LastName, StudentID],
[FirstName2, LastName2, StudentID2]]

How to take the StudentID for every students only and save it in new column?

like image 247
Cayza Angka Maulana Avatar asked Dec 10 '18 23:12

Cayza Angka Maulana


People also ask

How do you get the last string after a split?

pop() . The pop() method will return the last element from the split string array. Copied! The only argument we passed to the String.

How do you split a string on the last occurrence of a character in Python?

Use the str. rsplit() method with maxsplit set to 1 to split a string on the last occurrence of a delimiter, e.g. my_str. rsplit(',', 1) . The rsplit() method splits from the right, and only performs a single split when maxsplit is set to 1 .

Does Split () return a list?

The split() function returns the strings as a list.

Does string split preserve order?

Yes, . split() always preserves the order of the characters in the string.


1 Answers

Try the below solution:

item["x"]["y"].split(' ')[-1]
like image 82
Sumeet Thorat Avatar answered Oct 05 '22 00:10

Sumeet Thorat