2021年1月16日星期六

Check if ENTIRE pandas object column is a string

How can I check if a column is a string, or another type (e.g. int or float), even though the dtype is object?

(Ideally I want this operation vectorised, and not applymap checking every row...)

  import io  # American post code  df1_str = """id,postal  1,12345  2,90210  3,"""  df1 = pd.read_csv(io.StringIO(df1_str))  df1["postal"] = df1["postal"].astype("O")  # is an object (of underlying type float due to the null row 3)    # British post codes  df2_str = """id,postal  1,EC1  2,SE1  3,W2"""  df2 = pd.read_csv(io.StringIO(df2_str))  df2["postal"] = df2["postal"].astype("O")  # is an object (of underlying type string)  

Both df1 and df2 return object when doing df["postal"].dtype

  • However, df2 has .str methods, e.g. df2["postal"].str.lower(), but df1 doesn't.
  • Similarly, df1 can have mathematical operations done to it, e.g. df1 * 2

This is different to other SO questions. who ask if there are strings inside a column (and not the WHOLE column). e.g:

https://stackoverflow.com/questions/65756553/check-if-entire-pandas-object-column-is-a-string January 17, 2021 at 09:47AM

没有评论:

发表评论