RainerS
RainerS

Reputation: 23

Converting String to Numeric in pandas

I encountered a Problem after importing JSON-Data. Usually _to_numeric_ is working fine, but not in this Case. I have a column of strings Looking like this: 10.2100 10.2400 ...

I am using Jupyter Notebook. I have tried astype(), _infer_objects(), but still the data keeps behaving like string. I checked this by multiplying the column with 2 and get: 10.210010.2100 10.240010.2400 ...

This is the Code:

df = pd.DataFrame()
temp = pd.DataFrame()
for file in filelist:
    with open(path+file, "r", encoding="windows-1252") as f:
        xml_string = f.read()
        if xml_string.find("<") >= 0:
            json_string = convert_to_json(xml_string)
            flat = flatten_json.flatten(json.loads(json_string))
            temp = pd.DataFrame.from_dict(flat, orient='index').transpose()
            df = df.append(temp, sort=False)

df = df.reset_index()
pd.to_numeric(df.['Telegram_PRC_VALS_STP_2_VAL_0_NUM'])
df['Telegram_PRC_VALS_STP_2_VAL_0_NUM'] = df['Telegram_PRC_VALS_STP_2_VAL_0_NUM']*2
´´´

Any ideas? As this is my first Question in here I hope I am meeting your expectations on decently asking Questions.

Upvotes: 2

Views: 150

Answers (1)

jezrael
jezrael

Reputation: 862431

You have to reassign output of to_numeric back, because it is not inplace method:

df['Telegram_PRC_VALS_STP_2_VAL_0_NUM']=pd.to_numeric(df['Telegram_PRC_VALS_STP_2_VAL_0_NUM'])

Upvotes: 2

Related Questions