I use pandas and I have data and the data look like this
JavaScript
x
3
1
FirstName LastName StudentID
2
FirstName2 LastName2 StudentID2
3
Then I split it based on ‘space’ using str.split()
So the data will look like this in DataFrame
JavaScript
1
3
1
[[FirstName, LastName, StudentID],
2
[FirstName2, LastName2, StudentID2]]
3
How to take the StudentID for every students only and save it in new column?
Advertisement
Answer
Use a list comprehension to take the last element of each of the split strings:
JavaScript
1
2
1
ids = [val[-1] for val in your_string.split()]
2