0

I have this code and I can't delete a specific row from the DataFrame. How can I improve this code? I want to delete the values from that column that are not numeric. This way does not work because there are some missing columns that were deleted previously so I get

KeyError: '[1093] not found in axis'

Code:

i = 0
for row in new_dataset_2.GbCity:
    if isinstance(row,str):
        if not(row.isdigit()):
            new_dataset_2.drop(i)
    i+=1

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [Remove non-numeric rows in one column with pandas](https://stackoverflow.com/questions/33961028/remove-non-numeric-rows-in-one-column-with-pandas) – RoseGod Dec 13 '21 at 22:33
  • No... It does not remove every non-numeric since the ouput for : for row in new_dataset_2.GbCity: if isinstance(row,str): if not(row.isdigit()): print(row) new_dataset_2.drop(new_dataset_2.index[new_dataset_2["GbCity"]== row]) is 's' – Manuel Srgio Sokolov Ravasquei Dec 13 '21 at 22:39
  • 1
    Can you add a sample of the new_dataset_2 in order to be able to reproduce this question? (See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)). – RoseGod Dec 13 '21 at 22:40

1 Answers1

0

import math GbCity=new_dataset_2['GbCity'].astype("str").apply(lambda x:x if x.isnumeric() else math.nan) GbCity=GbCity.dropna()

arun bhat
  • 1
  • 1