将列数据类型从对象转换为数字会导致库文件导入错误

时间:2019-04-01 16:48:21

标签: python-3.x pandas dataframe

我正在尝试使用col_names列表中指定的列分析DataFrame中的某些数据。我一直在收集和解决错误,这要归功于StackOverflow社区,但是找不到这个错误了。

 File "pandas/_libs/lib.pyx", line 1925, in pandas._libs.lib.maybe_convert_numeric
 ValueError: Unable to parse string "import pandas as pd" at position 126000

那是我正在运行的代码:

import pandas as pd
file = "results/all_res.h5"

col_names = ['doa_in1','doa_in2','doa_est1','doa_est2','rho','seed','matl_chan']
df = pd.read_hdf(file,key='table')


for col in col_names[:-1]:
    print(df[col].dtype)
    df[col] = pd.to_numeric(df[col])

# Gives the obvious error I tried to fix with the for 
# a couple of lines above: I cannot do the absolute value of an object
df['error'] = abs(df['doa_est1'] - df['doa_in1'])+ abs(df['doa_est2'] - df['doa_in2'])

这是输出(带追溯)

object
Traceback (most recent call last):
File "pandas/_libs/lib.pyx", line 1892, in pandas._libs.lib.maybe_convert_numeric

ValueError: Unable to parse string "import pandas as pd"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "analyze_res.py", line 11, in <module>
df[col] = pd.to_numeric(df[col])
File "/usr/lib/python3.7/site-packages/pandas/core/tools/numeric.py", line 135, in to_numeric
coerce_numeric=coerce_numeric)
File "pandas/_libs/lib.pyx", line 1925, in pandas._libs.lib.maybe_convert_numeric
ValueError: Unable to parse string "import pandas as pd" at position 126000

老实说,我不知道如何解决pd.to_numeric()转换错误。在线我没有发现类似的错误。有人提示吗?

0 个答案:

没有答案