I have a dataframe with the following column:
print(df): Name James#4567547 Mick#5456535 Tash Liv#5468646 Nathan Chris You wil see some rows have the # and some dont. How can I loop through and retain all names and remove the # if present and anything after it. To get:
print(df): Name James Mick Tash Liv Nathan Chris I have tried:
if df['Name'].str.contains('#').any(): df['Name'] = df['Name'].str.split('#',1)[0] else: df['Name'] = df['Name'] But am getting a ValueError: Length of values does not match length of index at the str.split line. Any ideas? thanks!