This is because the last column is empty, so it is 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) df Out[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
If you cut your range to the last line, it works:
In [421]: for col in df.columns[2:-1]: df[col] = df[col].str.extract(r'(\d+\.*\d*)').astype(np.float) df Out[421]: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 'Name' 97.7 0 0 65 0 100 5 75 100 90 90 99 90 0 0 NaN
Alternatively, you can simply select the cols, which are the object dtype, and run the code (skip the first column, 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) df Out[428]: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 'Name' 97.7 0 0 65 0 100 5 75 100 90 90 99 90 0 0 NaN
source share