在熊猫中将字符串转换为数值

导入JSON数据后遇到问题。通常_to_numeric_可以正常工作,但在这种情况下则不能。我有一串字符串看起来像这样: 10.2100 10.2400 ...

我正在使用Jupyter Notebook。 我已经尝试过 astype(),_ infer_objects(),但是数据仍然表现得像字符串一样。我通过将列乘以2来检查并得到: 10.210010.2100 10.240010.2400 ...

这是代码:

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.
g109007 回答:在熊猫中将字符串转换为数值

您必须重新分配to_numeric的输出,因为它不是就地方法:

df['Telegram_PRC_VALS_STP_2_VAL_0_NUM']=pd.to_numeric(df['Telegram_PRC_VALS_STP_2_VAL_0_NUM'])
本文链接:https://www.f2er.com/3145145.html

大家都在问