pandas:数据类型的转换

版权声明:作者:Rookiekk 联系邮箱:[email protected] 欢迎转载或分享,但请务必声明文章出处。 https://blog.csdn.net/qq_18888869/article/details/85014125

今天做机器学习实验,我们查看数据类型把类别换成我们需要的合适的类别,为后边的处理做准备。把类别category类型转换成dummy/indicator变量。

首先介绍get_dummies():

pandas.get_dummies(dataprefix=Noneprefix_sep='_'dummy_na=Falsecolumns=None,sparse=Falsedrop_first=False):把类别category类型转换成dummy/indicator变量。

columns : list-like, default None

Column names in the DataFrame to be encoded. If columns is None then all the columns with object or category dtype will be converted.

参数列将会被编码。如果没有给出,那么所有为object或category类型的列将会被转换。

然后介绍astype():

DataFrame.astype(dtypecopy=Trueerrors='raise'**kwargs):转换为一个明确的类型

df.dtypes
Serial No.             int64
GRE Score              int64
TOEFL Score            int64
University Rating     object
SOP                  float64
LOR                  float64
CGPA                 float64
Research               int64
Admit                 object
dtype: object
df['University Rating'] = df['University Rating'].astype('object')
df.dtypes
Serial No.             int64
GRE Score              int64
TOEFL Score            int64
University Rating     object
SOP                  float64
LOR                  float64
CGPA                 float64
Research               int64
Admit                 object
dtype: object

猜你喜欢

转载自blog.csdn.net/qq_18888869/article/details/85014125