python construct 字符串_python – Pandas错误“只能使用带字符串值的.str访问器”

我有以下输入文件:

"Name",97.7,0A,0A,65M,0A,100M,5M,75M,100M,90M,90M,99M,90M,0#,0N#,

我正在阅读它:

#!/usr/bin/env python

import pandas as pd

import sys

import numpy as np

filename = sys.argv[1]

df = pd.read_csv(filename,header=None)

for col in df.columns[2:]:

df[col] = df[col].str.extract(r'(\d+\.*\d*)').astype(np.float)

print df

但是,我收到错误

df[col] = df[col].str.extract(r'(\d+\.*\d*)').astype(np.float)

File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 2241, in __getattr__

return object.__getattribute__(self, name)

File "/usr/local/lib/python2.7/dist-packages/pandas/core/base.py", line 188, in __get__

return self.construct_accessor(instance)

File "/usr/local/lib/python2.7/dist-packages/pandas/core/base.py", line 528, in _make_str_accessor

raise AttributeError("Can only use .str accessor with string "

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

这在pandas 0.14中运行正常,但在pandas 0.17.0中不起作用.

你可能感兴趣的:(python,construct,字符串)