简体   繁体   English

使用 Python 对 dataframe 中的特征列表进行分类编码的 For 循环

[英]For loop for categorical encoding on list of features in dataframe using Python

I am trying to figure out how to write a for loop to perform categorical encoding over a list of features.我试图弄清楚如何编写一个 for 循环来对特征列表执行分类编码。

df = ['TRY', 'LOC', 'OUTPUT', 'TYPE_A', 'SIGNAL', 'A-B  SPOT']

Currently, this is how I'm going about it, but it seems repetitive.目前,这就是我要做的事情,但它似乎是重复的。

obj_df["TRY"] = obj_df["TRY"].astype('category')
obj_df["TRY_cat"] = obj_df["TRY"].cat.codes

I tried following examples to write it and tried using a library as well but I think the logic is just off.我尝试了以下示例来编写它并尝试使用库,但我认为逻辑只是关闭了。

Is there a way to even do this based on how I'm going about it currently?有没有办法根据我目前的情况来做到这一点? Ideally, I would like to put it into a new dataframe as well.理想情况下,我也想将其放入新的 dataframe 中。

Thanks in advance!提前致谢!

Try this:尝试这个:

import pandas as pd
df = pd.DataFrame({'Color': {0: 'red', 1: 'green', 2: 'yellow', 3: 'navy_blue'},
'Shape': {0: 'square', 1: 'circle', 2: 'triangle', 3: 'cube'},
 'Description': {0: 'happy', 1: 'sad', 2: 'mad', 3: 'disgust'}})
cols = ['Color', 'Shape', 'Description']

df[cols] = df[cols].astype('category')
df[[col + '_cat' for col in cols]] = pd.concat([df[col].cat.codes for col in cols], axis=1)

print(df)
#        Color     Shape Description  Color_cat  Shape_cat  Description_cat
# 0        red    square       happy          2          2                1
# 1      green    circle         sad          0          0                3
# 2     yellow  triangle         mad          3          3                2
# 3  navy_blue      cube     disgust          1          1                0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM