将字符串转换为大熊猫中的数字

问题描述 投票:2回答:1

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

我正在使用Jupyter Notebook。我已经尝试过astype(),_ infer_objects(),但是数据仍然表现得像字符串一样。我通过将列乘以2来检查并得到:10.210010.210010.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.
python pandas dataframe
1个回答
2
投票

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

to_numeric
© www.soinside.com 2019 - 2024. All rights reserved.