convert a series or a column to integer.

1. Query the column
titanic_df['Embarked'][titanic_df['Embarked'] =='S'] = 0titanic_df['Embarked'][titanic_df['Embarked'] =='Q'] = 1titanic_df['Embarked'][titanic_df['Embarked'] =='C'] = 2titanic_df['Embarked'] = titanic_df['Embarked'].astype(np.int64)

2. map()
titanic_df['Embarked'] = titanic_df['Embarked'].map({'S': 0, 'Q': 1, 'C': 2})

3. apply()
def get_number(c): dic = {'S': 0, 'Q': 1, 'C': 2} return dic[c]titanic_df['Embarked'] = titanic_df['Embarked'].apply(get_number)

4. LabelEncoder()
from sklearn import preprocessinglbl = preprocessing.LabelEncoder()lbl.fit(np.unique(list(titanic_df['Embarked'].values) + list(test_df['Embarked'].values)))titanic_df['Embarked'] = lbl.transform(list(titanic_df['Embarked'].values))test_df['Embarked'] = lbl.transform(list(test_df['Embarked'].values))

5. pd.to_numeric()
This method introduced in version 0.17. Someone already asked a similar [question][1] .

And the list goes on ....
[1]:: http://stackoverflow.com/questions/15891038/pandas-change-data-type-of-columns

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容