i have a dataframe
| Name |
|---|
| Joe Smith |
| Jane Doe |
| Homer Simpson |
i am trying to format this to get to
| Name |
|---|
| Smith, Joe |
| Doe, Jane |
| Simpson, Homer |
i have this code, and it works for ~ 80% of users in my list but some users are not coming through right.
invalid_users = ['Test User', 'Test User2', 'Test User3']
for index, row in df_Users.iterrows():
gap_pos = df_Users["Name"][index].find(" ")
if gap_pos > 0 and row["Name"] not in invalid_users:
row["Name"] = df_Users["Name"][index][len(df_Users["Name"][index])-gap_pos+1:].strip() +', ' + df_Users["Name"][index][:gap_pos]
the users who are not coming through correctly, usually their last name is truncated somewhere - i.e. Simpson ==> mpson
What am I doing wrong here?
splitinstead,"Joe Smith".split(" ")will give you a list like this['Joe', 'Smith'][len(df_Users["Name"][index])-gap_pos+1:]? A simple[gap_pos+1:]should do (but better use one of the mentioned alternatives anyway).