I want to know which sklearn function/method will transform above two columns as with R=0, G=1 and B=2 and with C++ =0, Java=1, SQL=2 and Python=3 : Color: 0, 1, 2, 2, 1, 0, 2, 1, 1, 0, 1Skills: 1, 0, 2, 1, 3, 3, 2, 0, 1, 2, 1请告诉我该怎么做??推荐答案使用 Sckit-learn LabelEncoder() 方法Use Sckit-learn LabelEncoder() methodimport pandas as pdfrom sklearn.preprocessing import LabelEncoderdf = pd.DataFrame({'colors': ["R" ,"G", "B" ,"B" ,"G" ,"R" ,"B" ,"G" ,"G" ,"R" ,"G" ],'skills': ["Java" , "C++", "SQL", "Java", "Python", "Python", "SQL","C++", "Java", "SQL", "Java"]})def encode_df(dataframe): le = LabelEncoder() for column in dataframe.columns: dataframe[column] = le.fit_transform(dataframe[column]) return dataframe#encode the dataframeencode_df(df) 这篇关于使用 Python 或 Sklearn 中的整数值对具有字符串值的列变量进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 16:02