相关疑难解决方法(0)

DataFrame中的字符串,但dtype是对象

为什么Pandas告诉我我有对象,尽管所选列中的每个项都是一个字符串 - 即使在显式转换之后也是如此.

这是我的DataFrame:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 56992 entries, 0 to 56991
Data columns (total 7 columns):
id            56992  non-null values
attr1         56992  non-null values
attr2         56992  non-null values
attr3         56992  non-null values
attr4         56992  non-null values
attr5         56992  non-null values
attr6         56992  non-null values
dtypes: int64(2), object(5)
Run Code Online (Sandbox Code Playgroud)

其中五个是dtype object.我明确地将这些对象转换为字符串:

for c in df.columns:
    if df[c].dtype == object:
        print "convert ", df[c].name, " to string"
        df[c] = df[c].astype(str)
Run Code Online (Sandbox Code Playgroud)

然后,df["attr2"]仍然有dtype object,虽然type(df["attr2"].ix[0]揭示str,这是正确的. …

python types numpy series pandas

86
推荐指数
4
解决办法
4万
查看次数

标签 统计

numpy ×1

pandas ×1

python ×1

series ×1

types ×1