Home > Mobile >  How can I split my string and delete the first item for every row in my dataframe? key error 18
How can I split my string and delete the first item for every row in my dataframe? key error 18

Time:08-10

enter image description here

I keep getting key error 18 and dont know how to solve it. I am trying to split the values of a column and delete the first element of the list.

This is my code:

 last_name= []

for i in train.index:

#Split at space and delete the first name  

   name_id = test_df_bin['Name'][i].split(' ')
   del name_id[0]

 #list of last name
   last_name.append(name_id[0])


test_df_bin['Surname'] = last_name`

*Error messege:

KeyError Traceback (most recent call last) File ~\Anaconda\lib\site-packages\pandas\core\indexes\base.py:3621, in Index.get_loc(self, key, method, tolerance) 3620 try: -> 3621 return self._engine.get_loc(casted_key) 3622 except KeyError as err:

KeyError: 18*

CodePudding user response:

I strongly suggest against looping unless strictly necessary. In this case you can achieve your desired solution with:

test_df_bin['Surname'] = test_df_bin['Name'].str.split().str[1:]

If you'd like the output of the split() joined back into a single string, then:

test_df_bin['Surname'] = [' '.join(x) for x in test_df_bin['Name'].str.split().str[1:]]
  • Related