dataframe的字符类型dtypes为object,不为str

import pandas as pd
a=[1,2,3,1]
b=["a","b","c","d"]
df=pd.DataFrame({"A":a,"B":b})
print(df.dtypes)
答案:
  1. int64
  2. object

    The dtype object comes from NumPy, it describes the type of element in a ndarray. Every element in a ndarray must has the same size in byte. For int64 and float64, they are 8 bytes. But for strings, the length of the string is not fixed. So instead of save the bytes of strings in the ndarray directly, Pandas use object ndarray, which save pointers to objects, because of this the dtype of this kind ndarray is object.

Here is an example:

  • the int64 array contains 4 int64 value.
  • the object array contains 4 pointers to 3 string objects.
  • dataframe的字符类型dtypes为object,不为str_第1张图片

你可能感兴趣的:(python)