[英]How to convert categorical variable to numerical in pandas without increasing size of data?
I am trying to convert categorical data into numerical using get_dummies()
but the size of data increases from 1 X 1
to 1 X 22
because there are 22 different categorical variables. 我正在尝试使用get_dummies()
将分类数据转换为数值,但由于有22种不同的分类变量,因此数据的大小从1 X 1
增加到1 X 22
。
I used get_dummies()
from pandas. 我使用了熊猫的get_dummies()
。
Code: 码:
data['program_id'].unique()
cols_to_transform = data['program_id'].loc[:]
df_with_dummies = pd.get_dummies( data=cols_to_transform )
How can I convert categorical data of my column into numerical without increasing the size of data. 如何在不增加数据大小的情况下将列的分类数据转换为数字。
You can using factorize
您可以使用factorize
df['columns']=pd.factorize(df['columns'])[0]#df.columns.astype('category').cat.codes
Or LabelEncoder
from sklearn
或sklearn
LabelEncoder
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.