Pandas error "Can only use .str accessor with string values" Pandas error "Can only use .str accessor with string values" pandas pandas

Pandas error "Can only use .str accessor with string values"


It's happening because your last column is empty so this becomes converted to NaN:

In [417]:t="""'Name',97.7,0A,0A,65M,0A,100M,5M,75M,100M,90M,90M,99M,90M,0#,0N#,"""df = pd.read_csv(io.StringIO(t), header=None)dfOut[417]:       0     1   2   3    4   5     6   7    8     9    10   11   12   13  14  \0  'Name'  97.7  0A  0A  65M  0A  100M  5M  75M  100M  90M  90M  99M  90M  0#       15  16  0  0N# NaN  

If you slice your range up to the last row then it works:

In [421]:for col in df.columns[2:-1]:    df[col] = df[col].str.extract(r'(\d+\.*\d*)').astype(np.float)dfOut[421]:       0     1   2   3   4   5    6   7   8    9   10  11  12  13  14  15  160  'Name'  97.7   0   0  65   0  100   5  75  100  90  90  99  90   0   0 NaN

Alternatively you can just select the cols that are object dtype and run the code (skipping the first col as this is the 'Name' entry):

In [428]:for col in df.select_dtypes([np.object]).columns[1:]:    df[col] = df[col].str.extract(r'(\d+\.*\d*)').astype(np.float)dfOut[428]:       0     1   2   3   4   5    6   7   8    9   10  11  12  13  14  15  160  'Name'  97.7   0   0  65   0  100   5  75  100  90  90  99  90   0   0 NaN


I got this error while working in Eclipse. It turned out that the project interpreter was somehow (after an update I believe) reset to Python 2.7. Setting it back to Python 3.6 resolved this issue. It all resulted in several crashes, restarts and warnings. After several minutes of troubles it seems fixed now.

While I know this is not a solution to the problem posed here, I thought it might be useful for others, as I came to this page after searching for this error.


In this case we have to use the str.replace() method on that series, but first we have to convert it to str type:

df1.Patient = 's125','s45',s588','s244','s125','s123'df1 = pd.read_csv("C:\\Users\\Gangwar\\Desktop\\competitions\\cancer prediction\\kaggle_to_students.csv")df1.Patient = df1.Patient.astype(str)df1['Patient'] = df1['Patient'].str.replace('s','').astype(int)