简体   繁体   English

如何在不增加数据大小的情况下将大熊猫中的分类变量转换为数值?

[英]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. 如何在不增加数据大小的情况下将列的分类数据转换为数字。

Screenshot: 屏幕截图: 在此处输入图片说明

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.

相关问题 如何在 python pandas 的 for 循环中将分类数据转换为数值数据 - how to convert categorical data to numerical data in for loop in python pandas 如何将dtype分类变量转换为数值? - How to convert dtype categorical variable to numerical? 在 Python 中将数值数据转换为分类数据 - Convert numerical data to categorical in Python 如何将一系列数值数据转换为特定的分类数据? - How can convert a range of numerical data to a particular categorical data? 如何在不影响 DataFrame 中的数值数据的情况下对分类数据进行编码? - How encode categorical data without affecting numerical data in a DataFrame? python - 如何在带有分类和数值列的python中将每日数据转换为每周或每月? - How to convert daily data into weekly or monthly in python with categorical and numerical column? 如何在张量流中将数值分类数据转换为稀疏张量? - How to convert numerical categorical data into Sparse tensors in tensorflow? pandas 数据帧中的单独数字和分类变量 - separate numerical and categorical variable in pandas datframe 我想在 Python 中将分类变量转换为数值 - I want to convert the categorical variable to numerical in Python 在 Python 中将分类数据转换为数值数据 - Convert categorical data into numerical data in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM