将虚拟列添加到原始数据帧

时间:2014-04-22 01:19:19

标签: python pandas dataframe one-hot-encoding

我的数据框如下所示:

             JOINED_CO GENDER    EXEC_FULLNAME  GVKEY  YEAR  CONAME  BECAMECEO  REJOIN   LEFTOFC    LEFTCO  RELEFT    REASON  PAGE
CO_PER_ROL                                                                                                                                     
5622              NaN   MALE   Ira A. Eichner   1004  1992  AAR CORP   19550101     NaN  19961001  19990531     NaN  RESIGNED    79
5622              NaN   MALE   Ira A. Eichner   1004  1993  AAR CORP   19550101     NaN  19961001  19990531     NaN  RESIGNED    79
5622              NaN   MALE   Ira A. Eichner   1004  1994  AAR CORP   19550101     NaN  19961001  19990531     NaN  RESIGNED    79
5622              NaN   MALE   Ira A. Eichner   1004  1995  AAR CORP   19550101     NaN  19961001  19990531     NaN  RESIGNED    79
5622              NaN   MALE   Ira A. Eichner   1004  1996  AAR CORP   19550101     NaN  19961001  19990531     NaN  RESIGNED    79
5622              NaN   MALE   Ira A. Eichner   1004  1997  AAR CORP   19550101     NaN  19961001  19990531     NaN  RESIGNED    79
5622              NaN   MALE   Ira A. Eichner   1004  1998  AAR CORP   19550101     NaN  19961001  19990531     NaN  RESIGNED    79
5623              NaN   MALE  David P. Storch   1004  1992  AAR CORP   19961009     NaN       NaN       NaN     NaN       NaN    57
5623              NaN   MALE  David P. Storch   1004  1993  AAR CORP   19961009     NaN       NaN       NaN     NaN       NaN    57
5623              NaN   MALE  David P. Storch   1004  1994  AAR CORP   19961009     NaN       NaN       NaN     NaN       NaN    57
5623              NaN   MALE  David P. Storch   1004  1995  AAR CORP   19961009     NaN       NaN       NaN     NaN       NaN    57
5623              NaN   MALE  David P. Storch   1004  1996  AAR CORP   19961009     NaN       NaN       NaN     NaN       NaN    57

对于YEAR值,我喜欢将年份列(1993,1994 ...,2009)添加到原始数据框中,如果YEAR中的值是1992,那么1992列中的值应为1,否则为0。

我使用了一个非常愚蠢的for循环,但它似乎永远运行,因为我有一个大型数据集。 任何人都可以帮助我,非常感谢!

1 个答案:

答案 0 :(得分:44)

In [77]: df = pd.concat([df, pd.get_dummies(df['YEAR'])], axis=1); df
Out[77]: 
      JOINED_CO GENDER    EXEC_FULLNAME  GVKEY  YEAR    CONAME  BECAMECEO  \
5622        NaN   MALE   Ira A. Eichner   1004  1992  AAR CORP   19550101   
5622        NaN   MALE   Ira A. Eichner   1004  1993  AAR CORP   19550101   
5622        NaN   MALE   Ira A. Eichner   1004  1994  AAR CORP   19550101   
5622        NaN   MALE   Ira A. Eichner   1004  1995  AAR CORP   19550101   
5622        NaN   MALE   Ira A. Eichner   1004  1996  AAR CORP   19550101   
5622        NaN   MALE   Ira A. Eichner   1004  1997  AAR CORP   19550101   
5622        NaN   MALE   Ira A. Eichner   1004  1998  AAR CORP   19550101   
5623        NaN   MALE  David P. Storch   1004  1992  AAR CORP   19961009   
5623        NaN   MALE  David P. Storch   1004  1993  AAR CORP   19961009   
5623        NaN   MALE  David P. Storch   1004  1994  AAR CORP   19961009   
5623        NaN   MALE  David P. Storch   1004  1995  AAR CORP   19961009   
5623        NaN   MALE  David P. Storch   1004  1996  AAR CORP   19961009   

      REJOIN   LEFTOFC    LEFTCO  RELEFT    REASON  PAGE  1992  1993  1994  \
5622     NaN  19961001  19990531     NaN  RESIGNED    79     1     0     0   
5622     NaN  19961001  19990531     NaN  RESIGNED    79     0     1     0   
5622     NaN  19961001  19990531     NaN  RESIGNED    79     0     0     1   
5622     NaN  19961001  19990531     NaN  RESIGNED    79     0     0     0   
5622     NaN  19961001  19990531     NaN  RESIGNED    79     0     0     0   
5622     NaN  19961001  19990531     NaN  RESIGNED    79     0     0     0   
5622     NaN  19961001  19990531     NaN  RESIGNED    79     0     0     0   
5623     NaN       NaN       NaN     NaN       NaN    57     1     0     0   
5623     NaN       NaN       NaN     NaN       NaN    57     0     1     0   
5623     NaN       NaN       NaN     NaN       NaN    57     0     0     1   
5623     NaN       NaN       NaN     NaN       NaN    57     0     0     0   
5623     NaN       NaN       NaN     NaN       NaN    57     0     0     0   

      1995  1996  1997  1998  
5622     0     0     0     0  
5622     0     0     0     0  
5622     0     0     0     0  
5622     1     0     0     0  
5622     0     1     0     0  
5622     0     0     1     0  
5622     0     0     0     1  
5623     0     0     0     0  
5623     0     0     0     0  
5623     0     0     0     0  
5623     1     0     0     0  
5623     0     1     0     0  

如果您要删除YEAR列,则可以使用del df['YEAR']进行跟进。或者,在调用YEAR之前从df删除concat列:

df = pd.concat([df.drop('YEAR', axis=1), pd.get_dummies(df['YEAR'])], axis=1)