I m new to the field of Data Science, but I have a great passion for it. Here's a code I wrote for pre processing a data set. It works

import pandas as pd
from sklearn import svm

%matplotlib inline
import matplotlib.pyplot as plt

from sklearn.impute import SimpleImputer
import seaborn as sns; sns.set(font_scale=1.2)

X= stock.iloc[:,0:5].values
y= stock.iloc[:,5].values

#np.where(x.values >= np.finfo(np.float64).max)

g=sns.lmplot('alcohol','quality',data=stock,height=7, truncate=True, scatter_kws={"s":100})
imputer = SimpleImputer( strategy = "mean")
#my_imputer = SimpleImputer(missing_values=np.nan, strategy='mean')

imputer = imputer.fit(X[:,1:2])

imputer = imputer.fit(X[:,4:5])

I want to know what if i have both strings and numeric data in one column,how to pre process such data to have all numeric data.

  • 1
    $\begingroup$ what do you want to do with the string data? Turn it to NaNs, give it an identifying number (e.g. -1), or something else? $\endgroup$ – Mason Caiby Sep 12 '19 at 18:41
  • $\begingroup$ In the data set I have in one column both numeric data and string data, i want to specifically locate the rows in which string data is there and convert them to numeric form $\endgroup$ – Dulangi_Kanchana Sep 15 '19 at 8:59
  • $\begingroup$ will df[0] = df[0].apply(lambda x: int(x)) work for you? 0 is the name of the column $\endgroup$ – Mason Caiby Sep 16 '19 at 16:05

Normally pandas should identify numeric types automatically. If it doesn't in your case, there seems to be a formatting issue.

Instead of reading everything into string, I'd rather try to enable pandas to read the types directly in the correct form. First I'd try to pass the column types in a dict to dtype, as in:

pd.read_csv(file_name, dtype={'int_column_name': 'int32', 'float_column_name': 'float32'})

If that is not applicable, because the format is not recognized by pandas automatically, you might want to try some other options.

E.g. if it's just the decimal point or the thousand separator that is different in your data, you can set it over the corresponding keywords in read_csv (thousands and/or decimal).

If that isn't sufficient because you have some special formatting etc., you can also pass your own converter to parse the string into the data type you require for the column, like this as an example of how you could parse numeric values with a currency unit:

import re
def strip_off_currency(currency_string):
    if m is not None:
        return np.float32(m.group('value'))
        return np.NaN

pd.read_csv(file_name, dtype={'int_column_name': 'int32', 'float_column_name': 'float32'}, converters={'monetary_ammount': strip_off_currency})

The reason why I would prefer this way over reading everything into memory and processing it there is, that it needs less memory and probably also is faster if you process the values just once (at least if you don't need to pass converters).


Just a wild guess here...did you download the file incorrectly, perhaps. I've looked at this example before. I tested the code; everything worked fine for me. Please see the example here.


Also, download the data from here.


Feed back with any questions.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.