python - Pandas :获得假人

标签 python pandas dummy-variable

我有以下数据框:

   amount  catcode    cid      cycle      date     di  feccandid    type
0   1000    E1600   N00029285   2014    2014-05-15  D   H8TX22107   24K
1   5000    G4600   N00026722   2014    2013-10-22  D   H4TX28046   24K
2      4    C2100   N00030676   2014    2014-03-26  D   H0MO07113   24Z

我想为列 type 中的值创建虚拟变量。大约有 15 个。我试过这个:

pd.get_dummies(df['type'])

它返回这个:

           24A  24C  24E  24F  24K  24N  24P  24R  24Z
date                                    
2014-05-15  0    0    0    0    1    0    0    0    0
2013-10-22  0    0    0    0    1    0    0    0    0
2014-03-26  0    0    0    0    0    0    0    0    1

我想要的是为 Type

中的每个唯一值设置一个虚拟变量列

最佳答案

你可以试试:

df = pd.get_dummies(df, columns=['type'])

关于python - Pandas :获得假人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36285155/

相关文章:

python - 错误 : Command errored out with exit status 1 while installing pyaudio

python - 由于插入命令中的单引号而导致 psycopg2 语法错误

python - AppEngine 服务器无法导入 atom 模块

python - 在 Python 的 for 循环中使用 pd.get_dummies 创建虚拟变量

r - 从互斥虚拟变量创建分类变量

python - 有没有办法从多个字典中访问具有相同名称的键?

python - 将 pandas 中的对象序列转换为时间序列

python - 根据名称 pandas python 对某些列进行乘法和求和

Python Pandas ix 时间戳

python - 在 Pandas 中获取虚拟变量,其中行包含多个变量作为列表?