Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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*

>Solution :

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:]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading