How to remove a certain number of characters at the start of a string

Tags: ,



I have a dataset of NHL Free Agents, however they are numbered as a part of the name. I am trying to make “1. Alex Ovechkin” look like “Alex Ovechkin”. Basically just trying to delete the number, period, and space between.

Dataset shown here

I have used the following code to successfully delete the numbers for the first 10 entries, however at entry 11 I need to delete 4 characters instead of 3. The same goes for row 100, I need to delete 5 characters to delete the numbers, period, and space.

This is the code that I have been trying to use to know avail.

free_agents['Player'] = free_agents['Player'].str[3:]

This works for the first 10 entries, but after that there is a space from 11-100, and a period and a space for the rest.

I also tried the following code, which worked for the first 10, but deleted the rest of the entries.

free_agents['Player'] = free_agents['Player'][0:10].str[3:]

My last attempt was to make a for loop, but did not work.

for player in free_agents['Player']:
    if player in free_agents['Player'][0:100]:
        free_agents = free_agents['Player'].str[2:]
    else: 
        free_agents['Player'] = free_agents['Player'].str[4:]

I’ve ran out of ideas to try, and would love some help in finding the most efficient way to do this. Thanks so much!

Answer

split by . and get string index 1 of the output

df.Player=df.Player.str.split('.s').str[1]


Source: stackoverflow