print index and value if value is str in a numeric data type column pandas dataframe

Viewed 1088

I am new to data science and currently I'm exploring a bit further. I have over 600,000 columns of a data set and I'm currently cleaning and checking it for inconsistency or outliers. I came across a problem which I am not sure how to solve it. I have some solutions in mind but I am not sure how to do it with pandas.

I have converted the data types of some columns from object to int. I got no errors and checked whether it's in int and it was. I checked the values of one column to check for the factual data. This involves age and I got an error saying my column has a string. so I checked it using this method:

print('if there is string in numeric column',np.any([isinstance(val, str) for val in homicide_df['Perpetrator Age']])

Now, I wanted to print all indices and with their values and type only on this column which has the string data type.

currently I came up with this solution that works fine:

def check_type(homicide_df):
    for age in homicide_df['Perpetrator Age']:
        if type(age) is str:
            print(age, type(age))
check_type(homicide_df)

Here are some of the questions I have:

  1. is there a pandas way to do the same thing?
  2. how should I convert these elements to int?
  3. why were some elements on the columns did not convert to int?

I would appreciate any help. Thank you very much

1 Answers
Related