简体   繁体   English

如何识别数据框中的标称列?

[英]how to identify nominal columns in a data frame?

I have the following dataset (self made) . 我有以下数据集(自制)。

a , b , c , 1 , 1.3  ,d
q , w , e , 2 , 45.5 ,r
z , x , c , 1 , 76.09,f
z , x , e , 4 , 0.09 ,r

Here the last col is the class value . 这里的最后一个col是类值。 Now when i load the data into a dataframe (df) and apply 现在当我将数据加载到数据帧(df)并应用时

pandas.get_dummies(df)

i get output like this 我得到这样的输出

   0_a  0_q  0_z  1_b  1_w  1_x  2_c  2_e  3_1  3_2  3_4  4_0.09  4_1.3  \
0    1    0    0    1    0    0    1    0    1    0    0       0      1   
1    0    1    0    0    1    0    0    1    0    1    0       0      0   
2    0    0    1    0    0    1    1    0    1    0    0       0      0   
3    0    0    1    0    0    1    0    1    0    0    1       1      0  

here it is converting the fractional values as well . 这里它也在转换分数值。 if i specify the col numbers like this 如果我这样指定列号

df = pandas.get_dummies(df , columns=[0,1,2])

i can get the desired output 我可以得到想要的输出

   3      4  0_a  0_q  0_z  1_b  1_w  1_x  2_c  2_e
0  1    1.3    1    0    0    1    0    0    1    0
1  2   45.5    0    1    0    0    1    0    0    1
2  1  76.09    0    0    1    0    0    1    1    0
3  4   0.09    0    0    1    0    0    1    0    1

My question is , how can i do that without specifying the column numbers . 我的问题是,如何在不指定列号的情况下做到这一点。 Is it possible to identify the column numbers of only nominal data (not fraction) ? 是否可以仅识别名义数据(而不是分数)的列号?

IIUC we can use DataFrame.select_dtypes() method: IIUC我们可以使用DataFrame.select_dtypes()方法:

Source DF: 来源DF:

In [151]: df
Out[151]:
   0  1  2  3      4  5
0  a  b  c  1   1.30  d
1  q  w  e  2  45.50  r
2  z  x  c  1  76.09  f
3  z  x  e  4   0.09  r

Solution: 解:

In [155]: df.select_dtypes(['number']) \
            .join(pd.get_dummies(df.select_dtypes(exclude=['number'])))
Out[155]:
   3      4  0_a  0_q  0_z  1_b  1_w  1_x  2_c  2_e  5_d  5_f  5_r
0  1   1.30    1    0    0    1    0    0    1    0    1    0    0
1  2  45.50    0    1    0    0    1    0    0    1    0    0    1
2  1  76.09    0    0    1    0    0    1    1    0    0    1    0
3  4   0.09    0    0    1    0    0    1    0    1    0    0    1

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

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