Python changes the string object to a number

advertisements

i have a 880184*1 dataframe, the only column is either integer object or string object. I want to change all string object to number 0. It looks like below:

index               column
.....               ......
23155     WILLS ST / MIDDLE POINT RD
23156                          20323
23157    400 Block of BELLA VISTA WY
23158                          19090
23159     100 Block of SAN BENITO WY
23160                          20474

Now the problem is both number and string are 'object' type, I don't know how to change the string like object to 0 like below:

index                          column
.....                          ......
23155                            0
23156                          20323
23157                            0
23158                          19090
23159                            0
23160                          20474

Another problem is that the sample size is too large, making it too long to use for loops to fix row by row. I want to use something like:

df.loc[df.column == ...] = 0


You can convert the type to numeric with pd.to_numeric and pass errors='coerce' so that you would get NaN for the ones cannot be converted to numbers. In the end, you can replace the NaNs with zero:

df["column"] = pd.to_numeric(df["column"], errors="coerce").fillna(0)
Out[15]:
0        0.0
1    20323.0
2        0.0
3    19090.0
4        0.0
5    20474.0
Name: column, dtype: float64

If you want the integer values, add astype('int64') to the end:

df["column"] = pd.to_numeric(df["column"], errors="coerce").fillna(0).astype("int64")
Out[16]:
0        0
1    20323
2        0
3    19090
4        0
5    20474
Name: column, dtype: int64